In this article, we will look at how to install PostgreSQL and the pgAdmin4 panel in a Docker container using docker-compose.
But first, we need to update the OS packages.
apt update
Install the necessary packages and add a new repository:
apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Update the packages with the new repository:
apt update
Now let's install Docker itself.
apt-get install docker-ce docker-ce-cli containerd.io
Check the version:
docker --version
Check the status:
systemctl status docker
If it didn't start, then start it:
systemctl start docker
And add it to autostart.
systemctl enable docker
curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Set the execution rights.
chmod +x /usr/local/bin/docker-compose
Check how Docker-Compose was installed:
docker-compose --version
Create a *.yaml file for Docker-Compose.
To keep track of what we have installed in the future, let's create a separate folder for this project in the /home directory and navigate to it.
mkdir /home/postgres && cd /home/postgres
You can also use another directory to place this and other projects.
Let's use the repository to create the docker-compose.yaml file from the link https://hub.docker.com/_/postgres
Create the file docker-compose.yaml or docker-compose.yml,
vim docker-compose.yaml
And add the following code to it:
version: '3.8'
services:
db_postgres:
image: postgres
container_name: postgres
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: test_db
ports:
- "54320:5432"
volumes:
- local_pgdаta:/var/lib/postgresql/data
pgadmin4:
container_name: pgadmin4
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: root
ports:
- "5050:80"
volumes:
- pgadmin-dаta:/var/lib/pgadmin
volumes:
local_pgdаta:
pgadmin-dаta:
Where:
container_name: the name of your container;
POSTGRES_USER: User for the created database;
POSTGRES_PASSWORD: password for the user being created;
POSTGRES_DB: database name;
PGADMIN_DEFAULT_EMAIL: email/user for authorization in pgadmin;
PGADMIN_DEFAULT_PASSWORD: password for the pgadmin user;
5050:80: port on which pgadmin works.
Run our script (to do this, you need to be in the directory where our file was created. In this case, it is /home/postgres):
docker-compose up -d
Wait for the images to download and deploy.
Check:
docker-compose ps
or
docker ps
Now to connect to the database, you can use the container's IP with port 54320, or the container name postgres with port 54320 (IP:54320 or postgres:54320)
To view the logs, use the command:
docker logs -f postgres
To configure pgadmin — open a browser and go to the address — http://YOUR_IP_SERVER:5050/. In the connection details for the host name, specify the name of the postgreSQL container or the email you specified in PGADMIN_DEFAULT_EMAIL and the password root
Now let's connect the postgreSQL database server to pgadmin4.
Enter the data specified in docker-compose.yaml
Now you can create postgreSQL databases in pgadmin4.