How to Install pgAdmin4 on Ubuntu 24.04 Using Gunicorn and Nginx

Introduction

pgAdmin4 is a powerful, open-source administration and development platform for PostgreSQL. It provides a rich web-based interface for managing databases, running queries, visualizing schemas, and handling user roles. Whether you're deploying PostgreSQL locally or managing remote clusters, pgAdmin4 offers a secure and scalable way to interact with your data.

This guide shows you how to install pgAdmin4 on Ubuntu 24.04 using a virtual environment, Gunicorn, and Nginx. This setup is ideal if you want full control over your deployment.


Prerequisites

Before you begin:


Create Virtual Environment

  1. SSH into your server and create a working directory:

    CONSOLE
    $ mkdir ~/pgadmin4
    $ cd ~/pgadmin4
    
  2. Create and activate a Python virtual environment:

    CONSOLE
    $ python3 -m venv venv
    $ source venv/bin/activate
    

Install pgAdmin4

  1. Download the latest pgAdmin4 Python wheel:

    CONSOLE
    $ wget https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v9.7/pip/pgadmin4-9.7-py3-none-any.whl
    
  2. Install pgAdmin4 and dependencies:

    CONSOLE
    $ pip install pgadmin4-9.7-py3-none-any.whl
    $ pip install gunicorn flask flask-security flask-mail werkzeug
    
  3. Create a configuration file:

    CONSOLE
    $ nano config_local.py
    

    Paste the following:

    Python
    import os
    
    DATA_DIR = os.path.expanduser('~/pgadmin4/data')
    LOG_FILE = os.path.join(DATA_DIR, 'pgadmin4.log')
    SQLITE_PATH = os.path.join(DATA_DIR, 'pgadmin4.db')
    SESSION_DB_PATH = os.path.join(DATA_DIR, 'sessions')
    STORAGE_DIR = os.path.join(DATA_DIR, 'storage')
    
  4. Create required directories:

    CONSOLE
    $ mkdir -p ~/pgadmin4/data/sessions ~/pgadmin4/data/storage
    
  5. Test pgAdmin4 locally:

    CONSOLE
    $ gunicorn --bind 127.0.0.1:9000 --workers=1 --threads=25 pgadmin4.pgAdmin4:app
    

Configure Systemd

  1. Create a systemd service file:

    CONSOLE
    $ sudo nano /etc/systemd/system/pgadmin4.service
    

    Paste the following:

    INI
    [Unit]
    Description=pgAdmin4 Gunicorn Service
    After=network.target
    
    [Service]
    Type=simple
    User=your_server_username
    Group=your_server_username
    WorkingDirectory=/home/your_server_username/pgadmin4
    ExecStart=/home/your_server_username/pgadmin4/venv/bin/gunicorn --bind 127.0.0.1:9000 --workers=1 --threads=25 pgadmin4.pgAdmin4:app
    ExecReload=/bin/kill -s HUP $MAINPID
    KillMode=mixed
    Restart=always
    TimeoutStopSec=5
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
  2. Reload and start the service:

    CONSOLE
    $ sudo systemctl daemon-reexec
    $ sudo systemctl daemon-reload
    $ sudo systemctl enable pgadmin4
    $ sudo systemctl start pgadmin4
    
  3. Verify it's running:

    CONSOLE
    $ sudo systemctl status pgadmin4
    

Configure Nginx

  1. Create an Nginx site config:

    CONSOLE
    $ sudo nano /etc/nginx/sites-available/postgres.example.com
    

    Paste the following:

    Nginx Configuration File
    server {
        listen 443 ssl;
        server_name postgres.example.com;
    
        ssl_certificate /etc/letsencrypt/live/postgres.example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/postgres.example.com/privkey.pem;
    
        location / {
            proxy_pass http://127.0.0.1:9000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_redirect off;
        }
    }
    
  2. Enable the site and reload Nginx:

    CONSOLE
    $ sudo ln -s /etc/nginx/sites-available/postgres.example.com /etc/nginx/sites-enabled/
    $ sudo nginx -t
    $ sudo systemctl reload nginx
    

Conclusion

In this guide, you’ve installed pgAdmin4 on Ubuntu 24.04 using a virtual environment, Gunicorn, and Nginx. This modular setup gives you full control over your PostgreSQL admin interface, with production-grade security and scalability. You’ve also learned how to daemonize the service with systemd and reverse proxy it behind SSL — making your deployment clean, audit-friendly, and ready for global access.

  • Databases
  • Webservers
  • PHP
  • API
  • Python
  • VPS Guides
  • Network
  • AI
  • Node.js