Create and connect a PSQL database with docker

Steve
Mar 2, 2021

If you are working with PSQL in local development, most of the time it will be easier to host your database with docker. The main benefit I find in this solution is that it allows my co-developers and myself to launch my db in one command.

You will usually create it with something like it:

postgres:
container_name: postgres
restart: always
image: postgres:latest
volumes:
- ./postgres-data:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: "password"
POSTGRES_USER: "postgres"

The volumes linking allows you to keep the state of your PSQL db saved even after terminating your docker container.

To connect in local you just have to type the following command:

psql -h localhost -p 5432 -U postgres -W

Warning: hosting your database in a container is, at the time I write this article, not recommended for production environments.

--

--