DEV Community

Cover image for Setting Up a PostgreSQL Environment in Docker: A Step-By-Step Guide
Nathalia Friederichs
Nathalia Friederichs

Posted on

Setting Up a PostgreSQL Environment in Docker: A Step-By-Step Guide

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

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

Image description

PostgreSQL

Image description

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

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

Image description

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

Image description

The next step is to name the server.

Image description

Image description

  • 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

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.

Top comments (1)

Collapse
 
vandanadubey17 profile image
Vandana Dubey

Good information
Thank you