Skip to content

Instantly share code, notes, and snippets.

@bewithdhanu
Last active July 1, 2024 13:27
Show Gist options
  • Save bewithdhanu/2b9e0141a378ddaf0d6c295e5940b656 to your computer and use it in GitHub Desktop.
Save bewithdhanu/2b9e0141a378ddaf0d6c295e5940b656 to your computer and use it in GitHub Desktop.
Setting up pgAdmin4 with Docker

Setting up pgAdmin4 with Docker

To set up pgAdmin4 with Docker, follow these steps:

Installation and Docker Setup

  1. Install the required dependencies:

    sudo apt install apt-transport-https ca-certificates curl software-properties-common
  2. Import the Docker GPG key and add the Docker repository:

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt update
  3. Install and check Docker:

    sudo apt install docker-ce
    sudo systemctl status docker
    sudo usermod -aG docker ${USER}
    exit

Docker Configuration

  1. Verify Docker installation and test with a sample container:

    docker info
    docker run hello-world
  2. Install Docker Compose:

    sudo apt install docker-compose

pgAdmin4 Setup

  1. Create a directory for the pgAdmin4 setup:

    mkdir ~/pgadmin
    cd ~/pgadmin/
  2. Create a docker-compose.yml file and add the following content:

    version: '3'
    services:
      pgadmin:
        container_name: pgadmin4_container
        image: dpage/pgadmin4
        restart: always
        environment:
          PGADMIN_DEFAULT_EMAIL: ###ADMIN_EMAIL###
          PGADMIN_DEFAULT_PASSWORD: ###STRONG_PASSWORD###
        ports:
          - "5050:80"
        extra_hosts:
          - "host.docker.internal:host-gateway"
  3. Update PostgreSQL configuration:

    • Open postgresql.conf and set (sudo vi /etc/postgresql/14/main/postgresql.conf)

      listen_addresses = '*'
      
    • Open pg_hba.conf and add (sudo vi /etc/postgresql/14/main/pg_hba.conf):

      host    all             all             172.0.0.0/8              md5
      
    • Restart PostgreSQL:

      sudo service postgresql restart
  4. Start pgAdmin4 container:

    docker compose up -d
  5. Verify pgAdmin4 is running:

    curl localhost:5050

Apache Setup

  1. Make sure you have the necessary Apache modules enabled. Run the following commands on your Docker host:

    sudo a2enmod proxy
    sudo a2enmod proxy_http
    sudo a2enmod ssl
    sudo ufw allow 5430
    sudo ufw allow 5432
  2. Add a VitualHost to apache pgadmin4.conf (sudo vi /etc/apache2/sites-available/pgadmin4.conf)

    Listen 5430
    <VirtualHost *:5430>
        ServerName pro.24-7jobs.com
    
        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/server.crt
        SSLCertificateKeyFile /etc/apache2/ssl/server.key
            # SSLCertificateChainFile /etc/apache2/ssl/server-ca.crt
    
        ProxyPass / http://localhost:5050/
        ProxyPassReverse / http://localhost:5050/
    </VirtualHost>
    
  3. Enable VirtualHost

    sudo rm -r /etc/apache2/conf-available/pgadmin4.conf
    sudo rm -r /etc/apache2/conf-enabled/pgadmin4.conf
    sudo a2disconf pgadmin4.conf
    sudo a2ensite pgadmin4.conf
  4. Restart Apache to apply the changes. Run the following command to restart Apache:

    sudo service apache2 restart
  5. If on AWS, expose port 5430 in security group and restrict access.

That's it! Your pgAdmin4 setup with Docker is complete.

@bewithdhanu
Copy link
Author

bewithdhanu commented Aug 23, 2023

Here is the bash script for the same

#!/bin/bash

# Function to generate a random password
generate_password() {
    local length=${1:-12} # Default length is 12 if not provided
    local password=$(LC_ALL=C tr -dc 'A-Za-z0-9!@}^*(_<' < /dev/urandom | head -c "$length")
    echo "$password"
}

# Function to check if the string is a valid IP address
is_ip_address() {
    is_ip=$(echo "$1" | grep -qE "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
    if [ "$is_ip" = "1" ]; then
      return 1
    fi
    return 0
    
}

# Function to check if the string is a valid website URL
is_website_url() {
    is_url=$(echo "$1" | grep -qE "((http|https)://)(www.)?[a-zA-Z0-9@:%._\\+~#?&//=]*)")
    if [ "$is_url" = "1" ]; then
      return 1
    fi
    return 0
}


# Generate the password
password=$(generate_password 20)
echo "Generated password: $password"

# PostgreSQL Version
psqlVersion=$(psql --version | awk '{print int($3)}')
echo "Found PostgreSQL: $psqlVersion"
postgresConfig="/etc/postgresql/${psqlVersion}/main/postgresql.conf"
pgHbaConfig="/etc/postgresql/${psqlVersion}/main/pg_hba.conf"

# Prompt for password length
read -p "Enter website address (ex: pro.abc.com): " website

if is_ip_address "$website"; then
    type="IP"
elif is_website_url "$website"; then
    type="URL"
else
    echo "$website is neither a valid IP address nor a valid website URL."
    exit 1
fi

# Install dependencies
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

# Import Docker GPG key and add repository
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update

# Install Docker
sudo apt install -y docker-ce
sudo usermod -aG docker ${USER}

# Install Docker Compose
sudo apt install -y docker-compose

# Create pgAdmin4 setup directory
mkdir /home/ubuntu/pgadmin

# Create docker-compose.yml file
echo "version: '3'
services:
  pgadmin:
    container_name: pgadmin4_container
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: ${password}
    ports:
      - '5050:80'
    extra_hosts:
      - 'host.docker.internal:host-gateway'
" > /home/ubuntu/pgadmin/docker-compose.yml

# Update PostgreSQL configuration
line_to_add="listen_addresses = '*'"
if [ "$(tail -n 1 "$postgresConfig")" == "$line_to_add" ]; then
    echo "Line already exists in $postgresConfig"
else
    echo "$line_to_add" >> "$postgresConfig"
    echo "Line added to $postgresConfig"
fi
# Update pg_hba.conf
line_to_add="host    all             all             172.0.0.0/8              md5"
if [ "$(tail -n 1 "$pgHbaConfig")" == "$line_to_add" ]; then
    echo "Line already exists in $pgHbaConfig"
else
    echo "$line_to_add" >> "$pgHbaConfig"
    echo "Line added to $pgHbaConfig"
fi
sudo service postgresql restart

# Start pgAdmin4 container
cd /home/ubuntu/pgadmin && docker compose up -d

# Apache Setup
sudo a2enmod proxy proxy_http ssl

#whitelist port 5430,5432
sudo ufw allow 5430
sudo ufw allow 5432

# Create pgadmin4.conf file
if [ "$type" = "URL" ]; then
    echo "Listen 5430
    <VirtualHost *:5430>
        ServerName ${website}
    
        SSLEngine on
        SSLCertificateFile /etc/apache2/ssl/server.crt
        SSLCertificateKeyFile /etc/apache2/ssl/server.key
    
        ProxyPass / http://localhost:5050/
        ProxyPassReverse / http://localhost:5050/
    </VirtualHost>
    " | sudo tee /etc/apache2/sites-available/pgadmin4.conf
else
    echo "Listen 5430
    <VirtualHost *:5430>
        ServerName ${website}
    
        ProxyPass / http://localhost:5050/
        ProxyPassReverse / http://localhost:5050/
    </VirtualHost>
    " | sudo tee /etc/apache2/sites-available/pgadmin4.conf
fi
sudo rm -r /etc/apache2/conf-available/pgadmin4.conf
sudo rm -r /etc/apache2/conf-enabled/pgadmin4.conf
sudo a2disconf pgadmin4.conf
sudo a2ensite pgadmin4.conf

# Enable VirtualHost
sudo a2ensite pgadmin4.conf

# Restart Apache
sudo service apache2 restart

# Expose port 5430 in security group if on AWS

echo "Your pgAdmin4 setup with Docker is complete."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment