DEV Community

Cover image for How To Install and Run PostgreSQL using Docker ?

Posted on

How To Install and Run PostgreSQL using Docker ?

Installing, running and managing postgres in local-machine for development is not difficult anymore. Here is a simple way to get all at one place easily installed and configured within seconds with the help of docker.

Now skip all complex steps in installing and configuring PSQL to get started with local development and GUI to manage the DB. It's now easy to kick-start development of postgres based applications in a few seconds.

NOTE: This is to make the development process easy and however follow the conventional method of installing PSQL individually for production environment.

After following the instructions below, you will get the following installed in your machine:

  1. Postgres server running as a docker-container (which can be accessed by CLI, GUI or other application for development).
  2. Postgres container accessible through CLI.
  3. PgAdmin4 browser version to access Postgres server from GUI.


  1. Install docker
  2. … nothing else :)

Install and Configure PSQL using Docker:

Run the below command in linux or windows or mac machine from the terminal or command-prompt to pull PSQL from docker-hub.

docker run --name postgresql-container -p 5432:5432 -e POSTGRES_PASSWORD=somePassword -d postgres

In the above command replace :

  • Optional - postgresql-container with a preferable container name if necessary.
  • somePassword with a password to authenticate and connect to the postgres (in application with connection string as well as the PG-admin viewer).

Verify a new container created and running at with the below command.
docker ps -a


The PostgresQL is ready to connect and use.

The postgres server is now running in the IP of your local machine in 5432.

Install PG-admin using Docker:

Download the pgAdmin-4 browser version from docker-hub using the following command.

docker run --rm -p 5050:5050 thajeztah/pgadmin4

Now manage your postgres from the browser by launching http://localhost:5050 .


To connect the PSQL server in pgAdmin:


Enter the credentials to save and manage PSQL via GUI.
Host - The IP address of your machine
Password - Password used while creating the PSQL server with docker


Connecting to the PSQL server via CLI :

The steps below are to connect to the psql server from CLI :

  1. Find the docker-container-id in which the postgres is running using the below command. docker ps -a
  2. Run the below command to enter into the container (with the ID from step-1). docker exec -it <PSQL-Container-ID> bash
  3. Authenticate to start using as postgres user. psql -h localhost -p 5432 -U postgres -W
  4. Enter the password used while creating the PSQL server container.


Connecting to the PSQL server via application :

(example: JavaScript)

const { Client } = require('pg');

let client = new Client({
  connectionString: "postgresql://postgres:test1234@"

const connectDB = async () => {
  try {
    console.log('Connect to Postgres ...');
    await new Promise((resol, rej) => {
      client.query('Select now() as run_at;', (err, res) => {
        if(err) {
        } else {
          console.log(`Run at date-time : ${res.rows[0].run_at}`);
    await client.end();
    console.log('Execution Completed ...');
  } catch (err) {
    console.log('Error while Connecting DB !')


Top comments (16)

gkhan205 profile image
Ghazi Khan

Hi thanks for this article but when I do this. PGAdmin gets installed and started but when I try to access it on browser with localhost:5050 but it is not working. It shows site can't be reached.

shree_j profile image
ShreeJ • Edited

Ensure the docker container is up and running :
docker ps -a

gkhan205 profile image
Ghazi Khan

Yes it is running and shows that open , but still not able to access on browser.

Thread Thread
prab2112 profile image
Prabhakar Pandey

It looks like you missed to give port while running the pgadmin ?

Thread Thread
gkhan205 profile image
Ghazi Khan

I have given the port too, still it doesn't work.

Thread Thread
rajendragosavi profile image
Rajendra Gosavi

you are missing. -d flag. the in order to run in deamon mode you need to dive -d flag -
docker run --rm -d -p 5050:5050 thajeztah/pgadmin4

arcbjorn profile image

Thats really well written, good job 👏

marcosvrsdev profile image
Marcos Rezende

Hey great article! But why not use Docker for production environment?!

shree_j profile image

Its not about using docker for production environment.
As the DB is hosted in a container and not attached to any volume, once we delete the container (like docker rm <container_id>), the data in DB will be lost permanently.
Which will not be the way a production app should work.

dwaynebradley profile image
Dwayne Bradley

I agree with Marcos...nice article J Shree! :-)

It might also be helpful to show folks how to save their data even after running docker rm <container_id>. This is how I normally accomplish this:

First, create a local directory to hold the data:

mkdir -p /home/<your_user_id_here>/pgdata
Enter fullscreen mode Exit fullscreen mode

Then start PostgreSQL using a volume mount so the container will store the data in this newly created local directory:

docker run \
  -d \
  --name postgresql-container \
  -p 5432:5432 \
  -e POSTGRES_PASSWORD=somePassword \
  -v /home/<your_user_id_here>/pgdata:/var/lib/postgresql/data \
Enter fullscreen mode Exit fullscreen mode

Using this method, you can be safe in knowing that even if you accidentally run docker rm <container_id> that you can restart PostgreSQL again and have all of you data just as you left it previously.

Hope this helps!

lawrenceonen profile image
Onen Lawrence Lakuma

The reason you have to create a volume (which is mapped to your container) is to locally persist your data so that in the event you stop/restart your container, you can always have your data. So, your database stays though you will need the container running in order to access it.

valhuber profile image
Val Huber

May thanks for this...

I am about to switch to M1 mac. Is a separate ARM version required?

miladtehrany profile image
Milad Tehrany

Very useful, thank you so much

sitthykun profile image
Sitthykun Ly

Nice article

iason profile image

Great Stuf Thanks!

yonexbat profile image

How to find the ipaddress of the psql server for pgAdmin?:
Do this: docker inspect postgresql-container | grep IPAddress