Setting Up a PostgreSQL Environment in Docker: A Step-By-Step Guide
Tags: docker, postgres, programming, database
Published: 2024-02-05 21:01:07

Before anything else, let's begin with a brief explanation of what Docker is.

Docker is an open-source platform that makes it easy to create, distribute, and run applications within containers. Containers are isolated environments that contain everything necessary to run an application, including code, libraries, dependencies, and configurations. They are similar to virtual machines but are lighter and more resource-efficient.

Some of the key features and concepts related to Docker include:

1. **Images:** A Docker image is a package that contains everything needed to run an application, including source code, libraries, dependencies, and configurations. Images are used as templates to create containers.
2. **Containers:** A Docker container is a running instance of an image. They are isolated from each other and from the host machine, making them consistent and predictable in different environments.
3. **Dockerfile:** It's a configuration file that defines how to build a Docker image. It describes the steps to create the image, specifying what dependencies to install, how to configure the environment, and more.
4. **Docker Hub:** It's a public and private repository of Docker images maintained by Docker, Inc. You can find ready-to-use images and share your own images on Docker Hub.
5. **Container Orchestration:** Docker can be used in container orchestration, meaning you can manage clusters of containers to scale applications, load balance, and ensure high availability. Docker Swarm and Kubernetes are two popular tools for this purpose.

---


The first step in creating an environment in Docker is to install Docker on your machine. You can do this by visiting the Docker website at https://www.docker.com/products/docker-desktop/ and selecting the download for your operating system.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdl8x5zoo7zq3ycjjxo6.png)

To choose and find images, you can search on Docker Hub at https://hub.docker.com/search?q= for what you need.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rudculcr6kcazrkj0swo.png)

**PostgreSQL**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f87npnvh7ab17yfi6yqr.png)

To create a PostgreSQL environment in Docker, you can follow the steps below.

**Step 1: Download the PostgreSQL Image**

You can download the PostgreSQL image from Docker Hub at https://hub.docker.com/_/postgres/.

_docker pull postgres_

**Step 2: Create a PostgreSQL Container**

Now that you have the PostgreSQL image downloaded, you can create a container with the following command:

_docker run --name my-postgres -e POSTGRES_PASSWORD=my_password -d -p 5432:5432 postgres_

- "my-postgres" is the name of the container (you can choose a different name if you prefer).

- "my_password" is the password you want to set for the "postgres" user in PostgreSQL.

- The "-d" option runs the container in the background.

- The "-p 5432:5432" option maps port 5432 from the container to port 5432 on the host, allowing you to connect to PostgreSQL from the host.

**Step 3: Accessing PostgreSQL**

Now that the PostgreSQL container is running, you can access it using pgAdmin. To do this, you will need to download the pgAdmin image from Docker Hub at https://hub.docker.com/r/dpage/pgadmin4/ using the following code:

_docker pull dpage/pgadmin4_

Afterward, you need to create a container for running pgAdmin using the code:

_docker run --name test-pgadmin -p 15432:80 -e "PGADMIN_DEFAULT_EMAIL=my_email@test.com" -e "PGADMIN_DEFAULT_PASSWORD=my_password" -d dpage/pgadmin4_

- "test-pgadmin" is the name of the container being created.

- The "-p 15432:80" option maps port 15432, which is used for communication with pgAdmin, to port 80.

- "PGADMIN_DEFAULT_EMAIL" will be the login you use to access pgAdmin.

- "PGADMIN_DEFAULT_PASSWORD" will be the password you use to access pgAdmin.

Now you can access pgAdmin at https://localhost:15432, and you will see the pgAdmin interface.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sctkyir6ine9nogsf5p1.png)

After logging in with the defined email and password, the main panel will appear.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ggyxctfjiv18xew0gt79.png)

Now, the only thing left is to create a server to start using PostgreSQL. Follow these steps:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9pbv52uvnocnnge458f.png)

The next step is to name the server.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0tcwke8ewvrx8557lff8.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16tma17001i9i0ccsjyw.png)

- In the host name/address, put the name host.docker.internal.

- In the port, put the port you defined for your PostgreSQL container.

- In the username, enter the default user.

- In the password field, enter the password you defined, in this case, "my_password."

By following all these steps, you will have a screen similar to this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4nzjcxk0ez604duw3s7x.png)

You now have a PostgreSQL environment and pgAdmin running in Docker, and you can start creating databases and tables as needed.

---


By following the steps in this article, you can quickly create and set up Docker containers for PostgreSQL and pgAdmin, taking advantage of container virtualization for development and testing. This not only simplifies database administration but also makes collaboration and maintenance of consistent development environments easier.

It's important to mention that when using Docker containers in production environments, it's essential to consider security, performance, and scalability aspects. After all, this approach, while valuable for development, requires careful evaluation and adaptation to meet production demands.