Skip to content

Instantly share code, notes, and snippets.

@CaiqueMitsuoka
Created May 4, 2023 22:58
Show Gist options
  • Save CaiqueMitsuoka/627a783983bc6822141848d3b5200236 to your computer and use it in GitHub Desktop.
Save CaiqueMitsuoka/627a783983bc6822141848d3b5200236 to your computer and use it in GitHub Desktop.

Upgrade local postgres version

For context, I use one postgres instance on docker for local development.

# Example of docker-compose
# Remember to create the necessary volumes
# I use devbox for it.

version: "3"

networks:
  local_infra:
    external: true

volumes:
  postgres:
    external: true
  rabbitmq:
    external: true
  redis:
    external: true

services:

  postgres:
    image: postgres:10.6
    environment:
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: "123"
      PGDATA: /var/lib/postgresql/data/pgdata
    networks:
      - local_infra
    ports:
      - "127.0.0.1:5432:5432"
    volumes:
      - postgres:/var/lib/postgresql/data/pgdata

  rabbitmq:
    image: rabbitmq:3.7-management-alpine
    hostname: rabbitmq
    networks:
      - local_infra
    ports:
      - "127.0.0.1:5672:5672"
      - "127.0.0.1:15672:15672"

  redis:
    image: redis:latest
    networks:
      - local_infra
    ports:
      - "127.0.0.1:6379:6379"

Add the new pg version, and change ports in order to not conflict them. I changes the old pg to "127.0.0.1:5433:5432" to from the outside its binding to 5433.

And added pg 15, which its the newer one in the time of writing.

version: "3"

networks:
  local_infra:
    external: true

volumes:
  postgres:
    external: true
  postgres15:
    external: true
  rabbitmq:
    external: true
  redis:
    external: true

services:

  postgres:
    image: postgres:10.6
    environment:
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: "123"
      PGDATA: /var/lib/postgresql/data/pgdata
    networks:
      - local_infra
    ports:
      - "127.0.0.1:5433:5432"
    volumes:
      - postgres:/var/lib/postgresql/data/pgdata
      
  postgres15:
    image: postgres:15.2
    environment:
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: "123"
      PGDATA: /var/lib/postgresql15/data/pgdata
    networks:
      - local_infra
    ports:
      - "127.0.0.1:5432:5432"
    volumes:
      - postgres15:/var/lib/postgresql15/data/pgdata
  
  rabbitmq:
    image: rabbitmq:3.7-management-alpine
    hostname: rabbitmq
    networks:
      - local_infra
    ports:
      - "127.0.0.1:5672:5672"
      - "127.0.0.1:15672:15672"

  redis:
    image: redis:latest
    networks:
      - local_infra
    ports:
      - "127.0.0.1:6379:6379"

Note to different volume name, added it to the network with external: true and binding to the default port 5432.

Start the infrastructure. In case you need to create the volume docker volume create --name=postgres15. In case of devbox you may need to add to the config file.

Migrate your data

Grab your container_id with docker ps them:

docker exec -it container_id pg_dumpall -U postgres > dump.sql

With the dump file in hand

psql -U postgres -h 127.0.0.1 < dump.sql

I had the problem that the user postgres was create again with different password in the dump, which made me remove the creation/modification command from the dump to make it run. Which it pretty easy, just opened the file and remove the commands in the top of the file.

But because of that I had to docker volume rm postgres15 && docker volume create --name=postgres15 and restart the new pg.

Post update

Remove the container from docker, remove the container from the compose file, delete the valume and restart your infrastruture from the compose file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment