PostgreSQL is an advanced, enterprise-grade, open-source object-relational database management system (ORDBMS). Renowned for data integrity, ACID compliance, complex SQL queries, JSONB document storage, and Extensible PostGIS spatial features, PostgreSQL powers modern web applications, microservices, and analytical data warehouses.
Setting up a production-ready PostgreSQL environment on Ubuntu 24.04 LTS (Noble Numbat) or 22.04 LTS (Jammy Jellyfish) requires configuring the official PostgreSQL Global Development Group (PGDG) repository, securing the default postgres superuser, tuning pg_hba.conf network policies, and managing databases with pgAdmin 4.
Last updated: July 24, 2026
Key Takeaways
- Installing via the official PostgreSQL APT repository (apt.postgresql.org) guarantees access to the latest major releases (PostgreSQL 16/17) instead of outdated distribution packages.
- PostgreSQL runs under a dedicated system user named `postgres`. System administration tasks require switching to `sudo -i -u postgres`.
- Remote network access requires updating two configuration files: `postgresql.conf` (listen_addresses) and `pg_hba.conf` (client authentication rules).
- pgAdmin 4 can be installed as a desktop application or an Apache-hosted web management interface via the official pgadmin.org repository.
What are the system requirements for PostgreSQL on Linux?
Before installing PostgreSQL on Ubuntu, Debian, or Linux Mint, ensure your system satisfies these prerequisites:
- Operating System: Ubuntu 24.04 LTS, 22.04 LTS, Debian 12, or Linux Mint 22
- Privileges: User account with
sudoadministrative access - Disk Space: Minimum 500 MB for database binaries; additional storage dependent on database volume size
- Memory: Minimum 1 GB RAM (4 GB+ recommended for production query caching)
How do you install PostgreSQL from official PGDG APT repository?
While default Ubuntu repositories include older PostgreSQL builds, using the official PostgreSQL PGDG repository ensures you receive upstream security patches and the latest major versions.
Step 1: Install Dependencies and GPG Keyring
Open terminal (Ctrl+Alt+T) and import the official PostgreSQL signing key:
sudo apt update
sudo apt install -y curl ca-certificates gpg
# Create keyring directory if missing
sudo install -d /etc/apt/keyrings
# Import official PostgreSQL GPG key
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /etc/apt/keyrings/postgresql.gpgStep 2: Add PGDG Repository to APT Sources
Add the repository channel for your specific Ubuntu version:
echo "deb [signed-by=/etc/apt/keyrings/postgresql.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.listStep 3: Install PostgreSQL Server and Contrib Packages
Update your package index and install postgresql along with postgresql-contrib (which adds additional extensions like pg_trgm and uuid-ossp):
sudo apt update
sudo apt install -y postgresql postgresql-contribStep 4: Verify PostgreSQL Service Status
PostgreSQL starts automatically after installation. Confirm service status:
sudo systemctl status postgresqlTo ensure PostgreSQL launches automatically during system boot:
sudo systemctl enable postgresqlHow do you configure PostgreSQL users and databases?
PostgreSQL uses role-based access control and relies on system peer authentication by default.
Step 1: Access the PostgreSQL Shell (psql)
Switch to the default postgres system account and open psql:
sudo -i -u postgres
psqlDirect shortcut: sudo -u postgres psql
Step 2: Set Password for Default postgres Superuser
Inside the psql console, set a strong administrative password:
ALTER USER postgres WITH PASSWORD 'your_strong_admin_password';Step 3: Create a New Database and Application User
Avoid running production application queries under the postgres superuser. Create a dedicated user and database:
-- Create application user
CREATE USER myappuser WITH PASSWORD 'secure_user_password';
-- Create database owned by new user
CREATE DATABASE myappdb OWNER myappuser;
-- Grant database privileges
GRANT ALL PRIVILEGES ON DATABASE myappdb TO myappuser;Exit the psql shell:
\qHow do you enable remote network connections to PostgreSQL?
By default, PostgreSQL listens exclusively on the local loopback interface (127.0.0.1). Follow these steps to allow secure remote database connections.
Step 1: Configure Listening Addresses
Edit postgresql.conf (replace 16 with your installed version number):
sudo nano /etc/postgresql/16/main/postgresql.confLocate the CONNECTIONS AND AUTHENTICATION section and change:
# Change from 'localhost' to '*' to listen on all network interfaces
listen_addresses = '*'Save and close the file (Ctrl+O, Enter, Ctrl+X).
Step 2: Configure Client Authentication Rules (pg_hba.conf)
Edit the Host-Based Authentication configuration file:
sudo nano /etc/postgresql/16/main/pg_hba.confScroll to the bottom and add a rule allowing your client IP address or subnet using scram-sha-256 password authentication:
# IPv4 remote client connections
# TYPE DATABASE USER ADDRESS METHOD
host myappdb myappuser 192.168.1.0/24 scram-sha-256Step 3: Restart PostgreSQL Service and Allow Firewall Port
Restart PostgreSQL to apply configuration changes:
sudo systemctl restart postgresqlIf UFW firewall is active, allow incoming traffic on default port 5432:
sudo ufw allow 5432/tcpNever use 0.0.0.0/0 with trust authentication in pg_hba.conf. Always specify strict client IP subnets, enforce scram-sha-256 password hashing, and restrict port 5432 using UFW firewall rules or SSH tunneling.
How do you install pgAdmin 4 on Ubuntu?
pgAdmin 4 is the popular web and desktop administration tool for managing PostgreSQL instances.
Step 1: Add the pgAdmin 4 Repository
# Import pgAdmin GPG key
curl -fsSL https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /etc/apt/keyrings/pgadmin.gpg
# Add pgAdmin 4 repository
echo "deb [signed-by=/etc/apt/keyrings/pgadmin.gpg] https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" | sudo tee /etc/apt/sources.list.d/pgadmin4.listStep 2: Install pgAdmin 4
Install pgAdmin 4 in desktop mode, web mode, or both:
sudo apt update
# Install desktop application mode
sudo apt install -y pgadmin4-desktopFor web-only server mode hosted on Apache:
sudo apt install -y pgadmin4-web
sudo /usr/pgadmin4/bin/setup-web.shLaunch pgAdmin 4 from your application launcher, click Add New Server, and connect using host 127.0.0.1, port 5432, user postgres or myappuser, and your set password.
Frequently Asked Questions (PAA)
Is PostgreSQL free for commercial use?
Yes. PostgreSQL is released under the PostgreSQL License (a liberal OSI-approved license similar to BSD/MIT). You can use, modify, and distribute it freely for commercial applications without licensing fees.
What is the default port for PostgreSQL?
PostgreSQL uses TCP port 5432 by default. You can modify this port in /etc/postgresql/<version>/main/postgresql.conf.
How do I check which version of PostgreSQL is running?
Run psql --version in terminal for the CLI version, or execute SELECT version(); inside psql to display full server build details.
How do I backup and restore a PostgreSQL database?
To export a database to a SQL dump file, use pg_dump:
pg_dump -U myappuser -d myappdb > myappdb_backup.sqlTo restore a database dump:
psql -U myappuser -d myappdb < myappdb_backup.sqlRead Next
- How to Install Node.js on Ubuntu, macOS, and Windows
- How to Install Docker on Ubuntu, macOS, and Windows
- How to Monitor Linux Server CPU, Memory, and Disk Performance
Related Articles
Deepen your understanding with these curated continuations.

How to Install Redis Server & Redis CLI on Ubuntu & Linux (2026)
Step-by-step guide to installing Redis Server and Redis CLI on Ubuntu 24.04/22.04 LTS via official APT repository with security, password, and systemd setup.

How to Install Go (Golang) on Ubuntu & Linux (2026)
Complete guide to downloading, installing, and configuring Go (Golang) on Ubuntu 24.04/22.04 LTS with environment PATH setup and Go modules.

How to Self-Host Supabase with Docker Compose on Ubuntu (2026)
Complete guide to self-hosting Supabase (PostgreSQL, Kong API Gateway, Auth, Realtime, Storage, and Studio UI) on Ubuntu 24.04/22.04 LTS using Docker Compose.


