Definitive Notes on Docker and Postgres

Every once in a while I have to spin up a postgres dockerize instance to play around, and since 2017 it’s been a pain having to go through the docs just to get things working.
So now this is where I’m going to come back whenever I need to do that.

Creating the Container

docker run \
--name strapi-postgres \
-p 5432:5432 \
-e POSTGRES_PASSWORD=mycustompassword \
-e POSTGRES_USER=mycustomuser -d postgres
-e POSTGRES_DB=mycustomdatabase

Docs doesn’t mention the port, but here we use to bind it.

Connecting via

sudo apt install postgresql # for ubuntu/debian based

and then

psql -h localhost -p 5432 -U mycustomuser

Notice that we have to explicitly pass the localhost host, because by default Postgresql connects to a unix domain socket (I’m wondering what’s this)

Bonus: connecting from inside the container itself

docker exec -it strapi-postgres bash # opens shell in container
psql -U mycustomuser # won't even ask for password.

Jack Of All Trades