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
The first step is to have Docker installed in the system, obviously.
- If you are using Ubuntu: How to install Docker on Ubuntu 18.04?
- For CentOS 8: How to install Docker on CentOS 8?
- For CentOS 7: How to install Docker CE on CentOS 7?
- If you use Windows: How to install Docker in Windows 10?
- Or, if you are using Debian 10: How to install Docker on Debian 10?
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:
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 cominicate 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
depends_on clause helps us to relate it to the PotgreSQL 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.
:~$ sudo docker-compose.yml
When you finish running the program, you will be able to access PgAdmin4 from your web browser.
So, just type your credentials:
Next, you will see this:
And you can add a new server:
So, enjoy it.
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.