Running PostgreSQL on Docker

Running PostgreSQL on Docker

Learn how to run PostreSQL on Docker locally.

In earlier days hosting PostgreSQL was a challenge on local. It was eventually replaced by cloud solutions that offered free tiers like Heroku. But recently Heroku has decided to stop their free tier Postgres Service. I have been using the PostgreSQL Service of Heroku a lot for my hobby projects and side projects. But thought, with docker, it will be easy to use Postgres for local development, and then for integration, I can do something like Amazon RDS for PostgresSQL.

Pre-requisites

  • Docker Installed. (You can visit Docker and install the Docker client as per your Operating System.)
  • Docker Hub account. (Straightforward register process)

Running PostgreSQL Locally with Docker

The first step is to pull the Official Docker Image of PostgreSQL as below

docker pull postgres

1.JPG

The above statement will pull the official postgres docker image from the docker hub

Once successfully installed, verify if the image is available for local use by firing the below command

docker images

2.JPG

Execute the below command to run and start PostgreSQL within a docker container

docker run --name paaDB -p 5455:5432 -e POSTGRES_USER=paa -e POSTGRES_PASSWORD=paa123 -e POSTGRES_DB=paa_db -d postgres

3.JPG

  • --name : Specifies the name for the container.
  • -p : Port exposed for local consumption and mapped to default 5432 port of Postgres
  • -e: Setups different PostgreSQL Environment variables exposed by Image. From user, password to DB, and many more.
  • -d : Run Postgres image in detached mode, so it keeps running in the background. Once the command is run, you will see a container is created. If you want to verify what all environment variables, PG Version has been run you can execute the below command
docker exec paaDB env

4.JPG

You can alternatively, visit the docker client and see the container running as below

5.JPG

Environment Variables, Stats, Logs, and much more can be viewed via Docker Client UI as below

6.JPG

Connecting via PG Admin

You can use a PostgreSQL client such as PG Admin, which can be downloaded from here to connect your docker running PostgreSQL and create objects and perform other queries.

Just download, create a server, add a Database and play with it.

Voila, with minutes you can have PostGres SQL running on your local.

This article will be kept updated, as I learn and explore more things.

Thank you for reading, If you have reached it so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback and suggestions!

I would love to connect with you at Twitter | LinkedIn.

Did you find this article valuable?

Support Virendra Oswal by becoming a sponsor. Any amount is appreciated!