Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Play with PostreSQL Through Docker

This tutorial will show you how to deploy a PostgreSQL server locally with Docker and then log into it so that you can try it out. It would be a great way to see the performance difference parallel queries can make.

Related Posts

Steps

docker run -d \
  --name my_postgres \
  -v postgresql-data:/var/lib/postgresql/data \
  -e POSTGRES_PASSWORD=password \
  -p 54320:5432 postgres:12

Get the IP address

Unfortunatley, trying to connect to your postgresql server through localhost, or your machine's private IP address won't work. Instead you need to connect to the container's IP address which you find by running:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my_postgres

Now you have the IP address (in this case 172.17.0.2), use it in the following command to log in:

psql -U postgres -h 172.17.0.2

References

Last updated: 17th August 2020
First published: 17th August 2020