How to Run PostgreSQL on Docker Desktop

In this article, we will discuss various methods for running PostgreSQL and pgAdmin on the Docker Desktop.

About PostgreSQL

Due to its power and open source nature, PostgreSQL is a popular object-relational database management system (ORDBMS).

PostgreSQL has many prominent features such as:

  • It is cross-platform.
  • stores data securely.
  • It supports text, images, sounds, and video.
  • It includes programming interfaces for several languages.
  • supports some advanced SQL features.
  • It has the table inherence feature.
  • It is extendable by installing extensions.

How to Install PostgreSQL on Docker Desktop

Make sure that Docker Desktop is on your computer. Follow the link to find out how to install Docker Desktop.

Install PostgreSQL With CLI Command

Open a terminal and run the command:

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

The command above pulls the Postgres Docker image from Docker Hub, sets the POSTGRES_PASSWORD environment variable value to postgres, names the container postgres-docker, maps the container’s internal 5432 port to an external 5432 port, in order to enter it from outside, and enables running the Docker container in the background (-d).

Enter the CLI command to verify the container created:

docker ps

which returns the response:

PS C:\> docker ps
CONTAINER ID   IMAGE       COMMAND                  PORTS                     NAMES
a734da4ed339   postgres    "docker-entrypoint.s…"   0.0.0.0:5432->5432/tcp    postgres-docker

If you want to run PostgreSQL on a separate network, create the network with the command:

docker network create --driver bridge postgres-network

then run PostgreSQL on the network:

docker run --name postgres-docker --network postgres-network -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres

Install pgAdmin on Docker Desktop

You need a tool to browse/edit/maintenance your PostgreSQL database. One of the best tool is open source application pgAdmin. You can install it on Docker Desktop.

Run the CLI command to download the pgAdmin image to the Docker Desktop:

docker pull dpage/pgadmin4

Then run pgAdmin container by entering the command:

docker run -p 5050:80 \
    -e "PGADMIN_DEFAULT_EMAIL=user@email.com" \
    -e "PGADMIN_DEFAULT_PASSWORD=YourPassword" \
    -d dpage/pgadmin4

Enter localhost:5050 in a web browser and you can start to work with your PostgreSQL database:

pgAdmin

Follow the link for the details of the pgAdmin configuration.

Install PostgreSQL With Docker Compose File

The more convenient installation method is installing with a Docker composer file. You can install both PostgreSQL and pgAdmin at once, configure them in accordance with your project, and save the file for future use for other projects.

Prepare the file file docker-compose.yml with content:

version: '3.8'
services:
  db:
    container_name: pg_container
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: a_user
      POSTGRES_PASSWORD: a_password
      POSTGRES_DB: your_postgre_db
    volumes:
    - ./PostgresDb:/var/lib/postgresql
    ports:
      - "5432:5432"
  pgadmin:
    container_name: pgadmin4_container
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: an_admin_password
    ports:
      - "5050:80"

Execute the CLI command

docker compose up

And you will have PostgreSQL and pgAdmin installed and configured on the Docker desktop.


Was this helpful?

1 / 2

Leave a Reply 0

Your email address will not be published. Required fields are marked *