13 C
Texas
angeloma
Senior Writer and partner

Deploy PostgreSQL using Docker Compose

Hello. In this post, I will teach you how to deploy PostgreSQL using Docker compose. It will be brief but explained step by step.

As we well know PostgreSQL is one of the most powerful database management systems out there. So many applications will use it for its robustness and processing speed. In addition to other important features. PostgreSQL is also available as a Docker image with all the advantages that this can bring.

Briefly, I will tell you that Docker compose is more thought of production environments where compatibility should be as high as possible. This is precisely the great advantage of Docker images that we will be able to use in any supported system thanks to the technology of containers. Then, these steps can be done from any Linux distribution that has installed Docker and Docker Compose.

So, let us start.

Install Docker and Docker Compose on Linux

- Advertisement -

The first step is to have Docker installed in the system, obviously.

And using your distribution’s package manager, you can install Docker Compose. For example:

:~$ sudo apt docker-compose

For Debian, Ubuntu, and derivatives.

:~$ sudo yum install docker-compose
:~$ sudo dnf install docker-compose

Or, for RHEL, CentOS and derivatives.

Deploy PostgreSQL using Docker Compose

So let’s get down to business. First, create a dedicated folder for the target and navigate within it

:~$ mkdir postgresql
:~$ cd postgresql

Then, create a file called docker-compose.yml using a text editor. I will use nano.

:~$ nano docker-compose.yml

And we will have to add the following content:

version: '3'

services:

db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: "angelo123"
volumes:
- /var/postgresql/data:/var/lib/postgresql/data
networks:
- postgresql

pgadmin4:
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: "[email protected]"
PGADMIN_DEFAULT_PASSWORD: "angelo123"
ports:
- "80:80"
depends_on:
- db
networks:
- postgresql

networks:
postgresql:
driver: bridge
1.- Deploy PostgreSQL using docker compose
1.- Deploy PostgreSQL using docker compose

Now we will explain the contents of the file.

We will raise two services: PostgreSQL and PgAdmin4.

In the section that corresponds to PostgreSQL we define a password for the default postgres user. We also define a volume on our hard disk to get the data inside the container. Finally, we set it to use a network called postgresql. This will allow the containers to communicate with each other.

On the other hand, we have PgAdmin. In the environment section we set the default user and password. We will also use a defined port, in this case 80. The depends_on clause helps us to relate it to the PostgreSQL image and finally the network it will use.

At the end of the file, we specify the network as a bridge.

Now, run docker-compose.

:~$ docker-compose -f docker-compose.yml up

When you finish running the program, you will be able to access PgAdmin4 from your web browser. http://your-server:80

So, just type your credentials:

2.- PgAdmin log in screen
2.- PgAdmin log in screen

Next, you will see this:

3.- PgAdmin
3.- PgAdmin

And you can add a new server:

4.- PostgreSQL working
4.- PostgreSQL working

So, enjoy it.

Conclusion

Deploying PostgreSQL and PgAdmin using Docker compose is a task that requires some knowledge of the technology, but it is simple to do. In this post, you have learned how to do it without major problems.

In case of more information I leave you the links of the documentation of the images of PostgreSQL and PgAdmin4.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

2 COMMENTS

  1. I dont understand how can somone write a beautiful article, and then just mess up the most important part, which is to run the whole thing together: docker-compose -f docker-compose.yml up

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article