MeshWorld India LogoMeshWorld.
HowToPostgreSQLDatabaseLinuxUbuntupgAdmin6 min read

How to Install PostgreSQL & pgAdmin 4 on Ubuntu & Linux (2026)

Vishnu
By Vishnu
·
Vishnu
Updated by Vishnu
|Updated: Jul 24, 2026
How to Install PostgreSQL & pgAdmin 4 on Ubuntu & Linux (2026)

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 sudo administrative 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:

bash
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.gpg

Step 2: Add PGDG Repository to APT Sources

Add the repository channel for your specific Ubuntu version:

bash
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.list

Step 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):

bash
sudo apt update
sudo apt install -y postgresql postgresql-contrib

Step 4: Verify PostgreSQL Service Status

PostgreSQL starts automatically after installation. Confirm service status:

bash
sudo systemctl status postgresql

To ensure PostgreSQL launches automatically during system boot:

bash
sudo systemctl enable postgresql

How 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:

bash
sudo -i -u postgres
psql

Direct shortcut: sudo -u postgres psql

Step 2: Set Password for Default postgres Superuser

Inside the psql console, set a strong administrative password:

sql
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:

sql
-- 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:

sql
\q

How 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):

bash
sudo nano /etc/postgresql/16/main/postgresql.conf

Locate the CONNECTIONS AND AUTHENTICATION section and change:

text
# 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:

bash
sudo nano /etc/postgresql/16/main/pg_hba.conf

Scroll to the bottom and add a rule allowing your client IP address or subnet using scram-sha-256 password authentication:

text
# IPv4 remote client connections
# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    myappdb         myappuser       192.168.1.0/24          scram-sha-256

Step 3: Restart PostgreSQL Service and Allow Firewall Port

Restart PostgreSQL to apply configuration changes:

bash
sudo systemctl restart postgresql

If UFW firewall is active, allow incoming traffic on default port 5432:

bash
sudo ufw allow 5432/tcp

Security Warning: Production Remote Connections

Never 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

bash
# 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.list

Step 2: Install pgAdmin 4

Install pgAdmin 4 in desktop mode, web mode, or both:

bash
sudo apt update

# Install desktop application mode
sudo apt install -y pgadmin4-desktop

For web-only server mode hosted on Apache:

bash
sudo apt install -y pgadmin4-web
sudo /usr/pgadmin4/bin/setup-web.sh

Launch 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:

bash
pg_dump -U myappuser -d myappdb > myappdb_backup.sql

To restore a database dump:

bash
psql -U myappuser -d myappdb < myappdb_backup.sql

Share_This Twitter / X
Vishnu
Written By

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Vishnu
Updated By

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Enjoyed this article?

Support MeshWorld and help us create more technical content