Setting Up PostgreSQL with Docker

Setting Up PostgreSQL with Docker

Hello there! This guide will discuss how we can easily set up PostgreSQL using Docker.

Brief overview of PostgreSQL and Docker

PostgreSQL is a powerful open-source relational database management system known for its reliability and robust feature set. It is widely used in various applications due to its scalability and extensibility.

On the other hand, Docker is a popular platform for developing, shipping, and running applications using containerization technology. By combining PostgreSQL with Docker, developers can easily set up and manage database instances in isolated environments, making deployment and testing more efficient and consistent.

Pulling the PostgreSQL & PgAdmin images from Docker Hub

To setup PostgreSQL with Docker we need to pull the PostgreSQL docker image from the Docker Hub using the command

docker pull postgres

it will start pulling the latest image.

Now we have to start a PostgreSQL instance using the following command.

docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

Note: replace some-postgres with the name that you want for me it's my-postgres (optional)

Note: replace mysecretpassword with the password. It's required later.

Same way we will pull the PgAdmin image using the command

docker pull dpage/pgadmin4

docker run --name my_pgadmin -p 5050:80 -ePGADMIN_DEFAULT_EMAIL=enter your email id -e PGADMIN_DEFAULT_PASSWORD=add your password -d dpage/pgadmin4

**Note:**This is a single command. Ensure that the provided email address and password are needed to log in to the PgAdmin page.

Docker containers List

Now you can check the docker containers that are running. Postgres's default port is 5432.

Open pgadmin

Open any browser & navigate to localhost:5050 because PgAdmin is running on port 5050 .

Enter the email address and password for PgAdmin.

After successfully logging in, you will see the page where you need to create a new Server. Now click to the Add new server.

Add a name, for example: my-postgres.

Here you need to add the hostname (you will need to add the IP address).

Use the command to get the IP address

docker inspect my-postgres -f "{{json .NetworkSettings.Networks }}"

Change the username postgres and enter the password you created earlier to start the PostgreSQL instance.

After completing the steps above, click on the save button. Now, your PostgreSQL setup is complete.

Conclusion

By following the steps, you can seamlessly set up PostgreSQL with Docker. Using the power of Docker alongside PostgreSQL simplifies the deployment and testing processes, making them more consistent and effective.