# Rip & Tear: PostgreSQL Surgery

> Published  Dec 12 2024, last updated Feb 02 2025  
> By Ryan Fleck <hello@this-site> and written without LLMs!  
> Original post at <https://ryanfleck.ca/2024/postgres-surgery/>  
> An article of astonishing quality and insight. Happy Hacking!


After taking a leap of faith and upgrading my past-EOL Alpine Linux
server to the `latest-stable` release and packages, I was rewarded
with an almost seamless upgrade. Thanks to my dockerized personal
infrastructure, all my personal services came right back up without a
hitch - apart from two that depended on a local PostgreSQL database.

Yes, I was stupid to try and save resources by having a few containers
all depend on a local install of Postgres - but now I was in a real
pickle. Alpine had installed version 17 of PostgreSQL, and my database
was stuck way back on 13 - plus the binary had been purged. To no
avail, I attempted to use `apk` to install from the older repository,
but a dependency clash ensued.

**Docker to the rescue.** If I couldn't run Postgres 13 natively, I
imagined I could spin up a Docker container and point it towards the
original data to successfully run `pg_dump` and save the data. After a
little bit of copying and `chown`-ing from `/etc/postgresql` (symlinks
btfo) and `/var/lib/postgresql/13/data`, I was right back where I
started, and all my services were happy again, no longer screaming
*"no db! NO DB! NO DB!!!"*

Here's my `docker-compose` recipe:

```yml
services:
  postgres13:
    image: postgres:13.12
    #^ The version you are trying to rescue data from
    restart: unless-stopped
    ports:
      - 5432:5432
    environment:
      PGDATA: /data
      #^ Point the database to your bound volume
    volumes:
      - type: bind
        source: /home/the-service-username/postgres-data
        target: /data
        #^ Allow the container to see your data on disk
```

From here I was happily able to `pg_dump` the data and migrate
normally to a properly secured database.

Good times were had by all.

```
                                           Not possible
                                             to extract data
                                                 ^
                                                 |
  +----------------------------------------------+------------+
  | Host: Alpine Linux                           |            |
  |                                              |            |
  |              .m.+-             Postgres 13   |            |
  |             m-.m%------X-------(Deceased)----+            |
  |           .m.PS13--..                                     |
  |      -- --#%######+m.                                     |
  |    .   -%m OLD #m- .         +----------------------+     |
  |  .---+%+*m DATA -.+          | Dockerized           |     |
  |..m---###%#*#m%%--..          | Postgres 13          |     |
  | -+%##*.--m--.----------------+----o-------.         |     |
  |.m +-.m-.+m.. .               |            |         |     |
  | +- .-*    ..                 +------------+---------+     |
  +---++--+-----------------------------------|---------------+
                                              |
                                              V
                                          Data is freed!
```



> Thank you for reading!  
> Find more content at <https://ryanfleck.ca/>  
> Source page: <https://ryanfleck.ca/2024/postgres-surgery/>  
> Site index: [llms.txt](https://ryanfleck.ca/llms.txt)