PostgreSQL is a powerful open-source relational database management system (RDBMS) used by developers and enterprises worldwide. If you’re working on a Linux-based environment, setting up PostgreSQL is essential for handling complex queries and large-scale data. In this guide, we’ll walk you through the process of installing PostgreSQL on Linux step by step.

Step 1: Update Your System
Before installing PostgreSQL, it’s best to update your package list to ensure you have the latest software versions. Open your terminal and run:
sudo apt update && sudo apt upgrade -y # For Debian-based distros (Ubuntu, Debian)
sudo yum update -y # For RHEL-based distros (CentOS, Fedora)
Step 2: Install PostgreSQL
For Ubuntu/Debian-based Distributions
To install PostgreSQL, use the following command:
sudo apt install postgresql postgresql-contrib -y
For CentOS/Fedora (RHEL-based Distributions)
Use this command to install PostgreSQL:
sudo yum install -y postgresql-server postgresql-contrib
Once installed, initialize the database:
sudo postgresql-setup initdb
Then, enable and start the PostgreSQL service:
sudo systemctl enable postgresql
sudo systemctl start postgresql
Step 3: Verify PostgreSQL Installation
After installation, check if PostgreSQL is running:
sudo systemctl status postgresql
If it’s active, PostgreSQL has been successfully installed.
Step 4: Access PostgreSQL Database
PostgreSQL creates a default user named postgres. Switch to this user and open the PostgreSQL interactive shell:
sudo -i -u postgres
psql
To exit the PostgreSQL shell, type:
\q
Step 5: Enable Remote Access (Optional)
If you want to allow remote access to your PostgreSQL server, follow these steps:
Edit PostgreSQL Configuration File
Open the postgresql.conf file:
sudo nano /etc/postgresql/15/main/postgresql.conf # Ubuntu/Debian
sudo nano /var/lib/pgsql/data/postgresql.conf # CentOS/Fedora
Find the line:
listen_addresses = 'localhost'
Change it to:
listen_addresses = '*'
Modify pg_hba.conf File
Edit the pg_hba.conf file:
sudo nano /etc/postgresql/15/main/pg_hba.conf # Ubuntu/Debian
sudo nano /var/lib/pgsql/data/pg_hba.conf # CentOS/Fedora
Add the following line at the end:
host all all 0.0.0.0/0 md5
Save the file and restart PostgreSQL:
sudo systemctl restart postgresql
Manage PostgreSQL on Linux: Users, Databases, and Basic Commands
After the installation, create a separate user and database for each application instead of using the administrator account. Switch to the postgres system user and open the interactive terminal.
sudo -i -u postgres
psql
-- Inside psql
CREATE USER appuser WITH PASSWORD 'use-a-strong-password';
CREATE DATABASE appdb OWNER appuser;
GRANT ALL PRIVILEGES ON DATABASE appdb TO appuser; | psql command | What it does |
| \l | Lists all databases |
| \c appdb | Connects to the database appdb |
| \dt | Lists tables in the current database |
| \du | Lists users and roles |
| \q | Quits psql |
Back Up and Restore a PostgreSQL Database
# Create a backup
pg_dump -U appuser -d appdb -F c -f appdb.backup
# Restore a backup into an existing empty database
pg_restore -U appuser -d appdb appdb.backup Store backups in a second location, and test the restore process before you need it.
Check and Control the PostgreSQL Service
sudo systemctl status postgresql
sudo systemctl restart postgresql
sudo systemctl enable postgresql Security Warning for Remote Access
Allowing connections from every address is risky. Entries such as 0.0.0.0/0 in the pg_hba.conf file expose your database to the whole internet, and a weak password can lead to a compromise. Instead, follow these rules:
- Allow only the specific IP addresses or networks that need access.
- Use scram-sha-256 password authentication, which is stronger than md5 in modern versions.
- Use a firewall to block the PostgreSQL port, which is 5432 by default, except from trusted sources.
- Use a VPN or an SSH tunnel for remote administration.
- Keep PostgreSQL updated to receive security fixes.
Common PostgreSQL Installation Problems
- Peer authentication failed. Connect as the postgres system user, or change the authentication method for local users in
pg_hba.conf. - The service does not start. Check the log in
/var/log/postgresqlfor the reason, such as a configuration typo or an occupied port. - Cannot connect remotely. Confirm
listen_addresses, thepg_hba.confrules, and your firewall.
Tune PostgreSQL for Better Performance
The default configuration is conservative, so it works on small machines. If your server has more memory, you can adjust a few settings in postgresql.conf and restart the service. Change one setting at a time and measure the result.
| Setting | What it controls | Starting idea |
| shared_buffers | Memory used to cache data | About a quarter of the system memory on a dedicated server |
| work_mem | Memory for each sort or hash operation | Increase carefully, because it applies per operation |
| maintenance_work_mem | Memory for vacuuming and index creation | Larger values speed up maintenance jobs |
| max_connections | The number of simultaneous connections | Keep it modest, and use a connection pooler for busy applications |
Connect an Application to PostgreSQL
Most programming languages use a connection string that contains the user, password, host, port, and database name. Keep the string in an environment variable, not in your source code, and never publish it in a public repository.
postgresql://appuser:your-password@localhost:5432/appdb After the connection works, create regular backups and monitor the disk space, since a full disk is one of the most common reasons why a database stops.
Keep PostgreSQL Updated
Each major version of PostgreSQL is supported for a limited number of years, and minor releases include security and bug fixes. Check the official versioning policy, and plan upgrades before your version reaches the end of its support. Test the upgrade on a copy of your database first, because a major upgrade needs a dump and restore or the dedicated upgrade tool.
Our password and hash generator can help you create a strong password for each database user.
Final Thoughts: Need Help Installing PostgreSQL?
Setting up PostgreSQL on Linux can be tricky, especially if you need custom configurations or remote access. If this guide feels overwhelming, I can help you with installation, optimization, and database management. Contact me today for expert PostgreSQL setup services! 🚀
