Using Docker to work with multiple PostgreSQL versions
How to work with multiple PostgreSQL versions at the same time without losing my mind?
That's what we are trying to answer here. With Docker.
Docker PG Volumes
Data saved in a docker container are lost when that container is destroyed. We can save that data using docker volumes.
That's how we create a volume to use with a pg version 9.6:
docker volume create pgdata96
And that's how we create a volume for pg 13:
docker volume create pgdata13
Docker PG Containers
I like to map each pg version with a similar port. I follow this simple pattern:
version | port |
---|---|
postgres96 | 54*96* |
postgres12 | 54*12* |
postgres13 | 54*13* |
This command will download, run and setup for us a container with a postgres 9.6:
docker run --name postgres96 -e POSTGRES_PASSWORD=123456 -d -p 5496:5432 -v pgdata96:/var/lib/postgresql/data postgres:9.6
And the same for a postgres 13:
docker run --name postgres13 -e POSTGRES_PASSWORD=123456 -d -p 5413:5432 -v pgdata13:/var/lib/postgresql/data postgres:13
Look how I mapped the ports.
Create a DB
Let's create a database called todolist in the pg version 13:
docker exec -i postgres13 psql -U postgres -c "CREATE DATABASE todolist WITH OWNER=postgres;"
Or you can log in to the container e create it using psql:
docker exec -it postgres13 psql -U postgres
and then
$ CREATE DATABASE todolist;
Start the container
When you restart your machine, or for some other reasons unknown, you can start the containers like this:
docker start postgres13
If you need to run other containers you can run like this:
docker start postgres13 redis memcached
That is no need to run the command docker run
again.
Restore a dump
That's how you can restore a local dump file into a database:
docker exec -i postgres13 psql -U postgres -d todolist -f dump.sql
Top comments (0)