PostgreSQL with Docker — Quick Start

4 min readOct 29, 2020


Docker is changing how we distribute and install software and it is a very popular tool in many areas of software development as well. In this post, I will show you how to quickly get started with docker and postgreSQL.

I will try to keep this post very simple and will not cover complex workflows (those will be covered in later posts) and this will keep the discussion focus and help in easy learning. Now the idea is that you are gonna get, I don’t know, lights up the old neurons and creates one of those aha moments.

I am assuming that you already have installed docker on your machine and it is running.

Getting the Image

The following command will pull down the latest stable release Postgres image from the official Postgres docker hub repository.

>>docker pull postgres

Persist Data

Container data is gone once it is stopped and this is useful for certain situations (e.g. if you are running some database/integration testing and want to get rid of test data then its great). But, If we want to persist data generated by the Postgres instance running inside a container beyond the container’s lifecycle, we need to map a local mount point as a data volume to an appropriate path inside the container.

Running the Container

>>docker run -name pg-docker -e POSTGRES_PASSWORD=docker -e PGDATA=/tmp -d -p 5433:5432 -v ${PWD}:/var/lib/postgresql/data postgres:11

The following command variation uses another environmental variable to setup the database as well:

>>docker run --name pg-docker -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=sampledb -e PGDATA=/tmp -d -p 5433:5432 -v ${PWD}:/var/lib/postgresql/data postgres:11

Connect and use PostgreSQL

Once the container is up an running, connecting to it from an application is no different than connecting to a Postgres instance running outside a docker container. For example, to connect using psql we can execute

>> psql -h localhost -U postgres -d postgres

or u can use Azure Data Studio and make the connection as shown below:


Software Solutions Team Lead | Cloud Architect | Solutions Architect