OdooGap Blog / PostgreSQL on Docker

PostgreSQL on Docker

Connect several versions of Postgres

Sometimes you might need to have different versions of PostgreSQL running on your laptop due to different customer infrastructures. Recently I tried to restore a pg_dump of 9.5 into a 9.3 that’s currently on my development environment and errors started popping up on my screen.

After you have docker running on your local, just take it for a spin:

# First thing to do is run as root

_> sudo su

# List all running containers

_> docker ps # use (-a) for all containers

# Search containers

_> docker search postgres

# Pull a image into your local

_> docker pull postgres

Now you will be ready to start docker containers, exposing PostgreSQL ports on different numbers and then your local postgres instance (which is 5432).

# Lets start a container running PG 9.5 on port 54321
_> docker run --name pg95 -p 54321:5432 -d postgres:9.5
# Name of the container will be pg95
# Port 5432 will be tunneled to host 54321
# -d means that it will run on daemon mode

# Try now doing _> docker ps
53819ef289f4 postgres:9.5 "/docker-entrypoint. 14 hours ago Up 7 seconds>5432/tcp pg95

# Connect using command line tool :psql
_> psql -h localhost -p 54321 -U postgres

postgres=# select version();
PostgreSQL 9.5.3 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
(1 row)

Now you can use the usual command line tools like pg_dump, pg_restore, psql, createdb, dropdb. Just need to add -h localhost and -p 54321.

# You will need some user for odoo else server will not allow running with postgres user
_> createuser -h localhost -p 54321 -U postgres odoo

_> dropdb -h localhost -p 54321 -U postgres database_name
_> createdb -h localhost -p 54321 -U postgres -O odoo database_name
_> pg_restore -h localhost -p 54321 -U postgres -O -d database_name ~/db/file.dump

Now, to connect Odoo to your development environment it's quite easy, just add/change following in the conf file:

db_user = odoo
db_port = 54321
db_host = localhost