How to Run PostgreSQL and pgAdmin Using Docker

PostgreSQL is a widely used relational DBMS that stores, handles, and retrieves large amounts of data in an organized structure. pgAdmin is a free graphical user interface tool for organizing and creating databases. It is a feature-rich tool for managing the PostgreSQL database. It enables users to establish connections and easily switch between databases. Moreover, it supports numerous OS and PostgreSQL versions.

This blog will illustrate the procedure to set up/run PostgreSQL and pgAdmin with Docker.

How to Set Up/Run PostgreSQL and pgAdmin With Docker?

To set up/run PostgreSQL and pgAdmin using Docker, look at the below-mentioned steps:

- Pull/Download Postgres Docker Image
- Start and Run Postgres Container
- Pull/Download pgAdmin4 Docker Image
- Build and Run pgAdmin4 Container
- Verify Executing Containers
- Access pgAdmin4 on Browser
- Establish a Connection Between pgAdmin and Docker Postgres Instance

Step 1: Pull/Download Postgres Docker Image

Execute the below-provided “docker pull” command in Windows PowerShell to download the official Postgres image from the cloud repository (Docker Hub) to your local system:

docker pull postgres
img

Step 2: Start and Run Postgres Container

To create and run the Postgres container, utilize the “docker run --name -d <cont-name> -p 5432:5432 -e POSTGRES_PASSWORD=<password> postgres” command:

docker run --name   my-pgadmin -p 82:80 -e 'PGADMIN_DEFAULT_EMAIL=laiba@yahoo.com' -e 'PGADMIN_DEFAULT_PASSWORD=pass123' -d dpage/pgadmin4

Here:

- “-d” flag specifies that the container should execute in the background.
- “--name” option is used to assign the name for the container i.e. “postgresCont”.
- “-p” assigns the port for the container i.e. “5432:5432”.
- “-e POSTGRES_PASSWORD” configures the password to be “pass123”.
- “postgres” is the official Docker image:

img

This command has built and created the Postgres container.

Step 3: Pull/Download pgAdmin4 Docker Image

Run the below-listed command to download the “pgAdmin4” image in the local repository:

docker pull dpage/pgadmin4
img

Step 4: Build and Run pgAdmin4 Container

To build and execute the pgAdmin4 container, type out the “docker run --name <cont-name> -p 82:80 -e 'PGADMIN_DEFAULT_EMAIL=<email>' -e 'PGADMIN_DEFAULT_PASSWORD=<password>' -d dpage/pgadmin4” command:

docker run --name my-pgadmin -p 82:80 -e 'PGADMIN_DEFAULT_EMAIL=laiba@yahoo.com' -e 'PGADMIN_DEFAULT_PASSWORD=pass123' -d dpage/pgadmin4

Here:

- “my-pgadmin” is the container’s name.
- “-p” assigns the port for the container i.e. “82:80”.
- “-e PGADMIN_DEFAULT_EMAIL” sets the default email to be “laiba@yahoo.com”.
- “-e PGADMIN_DEFAULT_PASSWORD” configures the password to be “pass123”.
- “dpage/pgadmin4” is the official Docker image:

img

This command has created and started the pgAdmin4 container.

Step 5: Verify Executing Containers

Ensure that the PostgreSQL and pgAdmin4 containers are executing via provided command:

docker ps
img

It can be observed that both containers are running efficiently.

Step 6: Access pgAdmin4 on Browser

Now, navigate to http://localhost:82/ to access the pgAdmin4 interface and provide the required login credentials that we have configured in the previous step:

img

Upon doing so, it will redirect us to the dashboard:

img

This indicates that PostgreSQL and pgAdmin4 have been successfully installed using Docker.

Step 7: Establish a Connection Between pgAdmin and Docker Postgres Instance

Right-click on the “Servers” option, then select the “Register” option to add the new server:

img

After that, in the “General” menu, specify the name of the server. For instance, we have named it “PostgreSQL”:

img

Next, set the hostname or address, port, username, and password in the “Connection” menu and click on the “Save” button:

img

Upon doing so, the connection between the pgAdmin and Postgres container instance will be established. Now, users can use and access PostgreSQL:

img

We have efficiently explained the method to run PostgreSQL and pgAdmin using Docker.

Conclusion

To set up/run PostgreSQL and pgAdmin using Docker, download the official Postgres and pgAdmin4 images from Docker Hub. Then, build and start the Postgres and pgAdmin4 containers. Next, verify the executing containers. After that, access pgAdmin4 on the browser and establish a connection between pgAdmin and the Docker Postgres instance. This blog has illustrated the procedure to set up/run PostgreSQL and pgAdmin with Docker.