Managing Postgres database locally with Docker

How to use Docker to run and manage a local Postgres database for development.

Gone are the days when I’d install a database engine on my local machine to do some development. Whenever doing any database development, we should be in a position to have migration scripts as well as data seeding. Being able to teardown and spin up a database is good to practice often on any environment including local.

Welcome docker.

The following docker command is pretty neat because it will create everything you need to get Postgres running on your development machine.

docker run -e POSTGRES_USER=<dbuser> -e POSTGRES_PASSWORD=<dbpassword> -e POSTGRES_DB=<dbname> -p 5432:5432 --name <dbname> -d postgres

What’s cool about the Postgres image is that it already has a volume configured which means that even if you turn off your machine, the data will be retained. Next time you start docker, it’s all there.