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 Compose
version: "3"
services:
db:
image: postgres:15.3-bookworm
container_name: db
restart: always
ports:
- "5432:5432"
volumes:
- db-data:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_DB=${POSTGRES_DB}
volumes:
db-data:
driver: local
You will need to create a .env
with the following content (filling in values):
POSTGRES_PASSWORD="somethingHere"
POSTGRES_USER="admin"
POSTGRES_DB="some_db_name"
Pure Docker Command
Or if you wish to manually run through docker command, you could do:
docker run -d \
--name my_postgres \
-v postgresql-data:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=password \
-p 54320:5432 postgres:13
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
First published: 17th August 2020