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 -p to bind it.

Connecting via psql

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)

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

Jack Of All Trades

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store