SIGN IN / UP
    opened image

    In this article, we will look at how to install PostgreSQL and the pgAdmin4 panel in a Docker container using docker-compose.
     

    Let's install Docker

    But first, we need to update the OS packages. 
     

    apt update

    We will 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"

    We will 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

    Let's check the version:
     

    docker --version



    Let's check the status:

     

     

     

    systemctl status docker

     



    If it hasn't started, we start it:

     

     

     

     

    systemctl start docker
    

     


    And add it to autostart.

     

     

     

     

    systemctl enable docker
    

     


     

     

    Let's install Docker-Compose

     

     

     

     

    curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    

     


    We set the execution permissions. 

     

     

     

     

    chmod +x /usr/local/bin/docker-compose
    

     


    Let's check how Docker-Compose was installed:

     

     

     

     

    docker-compose --version
    

     



    Let's 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. 

    We will use the repository to create the docker-compose.yaml file at the link https://hub.docker.com/_/postgres

    Create a 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_pgdata:/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-data:/var/lib/pgadmin
    volumes:
      local_pgdata:
      pgadmin-data:
    
    

     


    Where:

    container_name: the name of your container;
    POSTGRES_USER: User for the created database;
    POSTGRES_PASSWORD: password for the user we are creating;
    POSTGRES_DB: name of the database;
    PGADMIN_DEFAULT_EMAIL: email/user for logging into pgadmin;
    PGADMIN_DEFAULT_PASSWORD: password for the pgadmin user;
    5050:80: port on which pgadmin works.

    Let's run our script (for 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.

    Let's 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 name of the container postgres with port 54320 (IP:54320 or postgres:54320) 

    To view the logs, use the command: 

     

     

     

     

    docker logs -f postgres
    

     



    To configure pgadmin — open your browser and go to the address — http://YOUR_IP_SERVER:5050/. In the connection details, specify the hostname as 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 you specified in docker-compose.yaml


     Now you can create PostgreSQL databases in pgadmin4.

    We also suggest considering other useful articles: