Skip to content

Install Postgres & Pgadmin4 with Nginx in docker

For personal learning purposes, running the PostgreSQL and the PgAdmin4 in docker is an easy and good way. And for some reason, we may want to use Nginx to proxy the PostgreSQL and the Pgadmin4.

Here is my configuration.

  1. Run the PostgreSQL in the docker

Bash
docker run --name postgres \
        -e POSTGRES_USER=sa \
        -e POSTGRES_PASSWORD=sa \
        -e PGDATA=/var/lib/postgresql/data/pgdata \
        -e POSTGRES_HOST_AUTH_METHOD=scram-sha-256 \
        -v /root/documents/postgresql/data:/var/lib/postgresql/data \
        -p 5432:5432 \
        --user "$(id -u):$(id -g)" \ # optional, depends on environment
        -d postgres
2. Run the PgAdmin4 in the Docker

Bash
docker run -p 8090:8090 --name pgadmin4 \
    -e 'PGADMIN_LISTEN_PORT=8090' \
    -e 'PGADMIN_DEFAULT_EMAIL=<Email>' \
    -e 'PGADMIN_DEFAULT_PASSWORD=<Password>!' \
    -e 'PGADMIN_CONFIG_ENHANCED_COOKIE_PROTECTION=True' \
    -e 'PGADMIN_CONFIG_LOGIN_BANNER="Authorised users only!"' \
    -e 'PGADMIN_CONFIG_CONSOLE_LOG_LEVEL=10' \
    -d dpage/pgadmin4
3. Configure the Nginx

  • Configure the Pgadmin4

    Bash
      server {
        # pgadmin4
        location /pgadmin4/ {
                      proxy_set_header X-Script-Name /pgadmin4;
                      proxy_set_header Host $host;
                      proxy_pass http://localhost:8090/;
                      proxy_redirect off;
        }
      }
    
  • Configure the PostgreSQL

    Bash
    # PostgreSQL
    stream {
        upstream postgres {
            server localhost:5432;
        }
    
        server {
            listen 5432 so_keepalive=on;
            proxy_pass postgres;
        }
    }