PostgreSQL installation on Linux typically begins with updating your system’s repository list. If you’re wondering how to install postgresql in linux, you’ve come to the right place. This guide walks you through every step, from prerequisites to verification, using simple commands and clear explanations. Whether you’re a developer or a system administrator, you’ll have PostgreSQL running in no time.
PostgreSQL is a powerful, open-source relational database system. It’s known for reliability, extensibility, and SQL compliance. Linux users often prefer it for production environments. But getting it installed correctly matters—especially if you’re new to the platform.
Let’s start with the basics. You’ll need a Linux distribution like Ubuntu, Debian, CentOS, or Fedora. Most commands work across these, but I’ll note differences where they exist. You’ll also need sudo or root access to install packages.
First, update your package repository. This ensures you get the latest version available. Open a terminal and run:
sudo apt update # For Debian/Ubuntu
sudo yum update # For CentOS/RHEL 7
sudo dnf update # For Fedora/CentOS 8+
Now you’re ready to install. The process is straightforward, but choosing the right version matters. Some distributions include older PostgreSQL versions. For the latest features, you might want to add the official PostgreSQL repository.
Prerequisites For PostgreSQL Installation
Before you begin, check your system. You need a stable internet connection and enough disk space. PostgreSQL requires at least 1 GB of RAM for basic operations, though more is better for production workloads.
Verify your Linux version with:
lsb_release -a # For Debian/Ubuntu
cat /etc/os-release # For most distributions
Make sure your system is up to date. Run the update command for your package manager. This prevents dependency conflicts during installation.
You’ll also need basic command-line skills. If you can run commands with sudo, you’re good. No advanced Linux knowledge required.
How To Install Postgresql In Linux
Now let’s dive into the actual installation. I’ll cover multiple methods: using default repositories, adding the official PostgreSQL repo, and compiling from source. Choose the one that fits your needs.
Method 1: Install Using Default Repositories
This is the simplest method. Your distribution’s package manager includes PostgreSQL. It may not be the latest version, but it’s stable and easy to set up.
For Ubuntu or Debian, run:
sudo apt install postgresql postgresql-contrib
The postgresql-contrib package adds extra utilities and extensions. It’s recommended for most users.
For CentOS or RHEL 7, use:
sudo yum install postgresql-server postgresql-contrib
For Fedora or CentOS 8+, use:
sudo dnf install postgresql-server postgresql-contrib
After installation, you need to initialize the database cluster. This creates the default data directory and configuration files. Run:
sudo postgresql-setup --initdb # For CentOS/Fedora
# For Ubuntu/Debian, initialization happens automatically
Then start the PostgreSQL service:
sudo systemctl start postgresql
sudo systemctl enable postgresql # Start on boot
Check the status to confirm it’s running:
sudo systemctl status postgresql
You should see “active (running)” in green. If not, check the logs for errors.
Method 2: Install From Official PostgreSQL Repository
For the latest version, use the official PostgreSQL repository. This gives you access to newer features and security patches. The process varies by distribution.
First, add the repository. For Ubuntu 22.04 (Jammy), run:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Then import the repository key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Update your package list:
sudo apt update
Now install PostgreSQL 16 (or your preferred version):
sudo apt install postgresql-16
For CentOS or RHEL, add the repository manually. Visit the PostgreSQL Yum Repository page for instructions. The process involves downloading an RPM and installing it.
After installation, the service starts automatically. Verify with:
sudo systemctl status postgresql-16
Note that the service name includes the version number when using the official repo.
Method 3: Compile From Source
Compiling from source gives you full control. It’s useful for custom configurations or older systems. But it’s more complex and time-consuming.
First, download the source code from the PostgreSQL website. Use wget or curl:
wget https://ftp.postgresql.org/pub/source/v16.1/postgresql-16.1.tar.gz
Extract the archive:
tar -xzf postgresql-16.1.tar.gz
cd postgresql-16.1
Install build dependencies. For Ubuntu:
sudo apt install build-essential libreadline-dev zlib1g-dev
For CentOS:
sudo yum groupinstall "Development Tools"
sudo yum install readline-devel zlib-devel
Configure the build:
./configure
You can add options like --prefix=/usr/local/pgsql to set the installation directory. Then compile:
make
This takes a few minutes. After it finishes, install:
sudo make install
Create a system user for PostgreSQL:
sudo useradd -m postgres
sudo passwd postgres
Initialize the database cluster:
sudo -u postgres /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
Start the server:
sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start
This method is more advanced. Use it only if you need specific customizations.
Post-Installation Configuration
After installation, you need to configure PostgreSQL. This includes setting up authentication, enabling remote access, and creating databases.
Accessing PostgreSQL
PostgreSQL creates a default user called postgres. Switch to this user to manage the database:
sudo -i -u postgres
Now you can access the PostgreSQL prompt:
psql
You’ll see a prompt like postgres=#. Type \q to exit.
To create a new user, run:
CREATE USER myuser WITH PASSWORD 'mypassword';
Create a database:
CREATE DATABASE mydb OWNER myuser;
Grant all privileges:
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Configuring Authentication
Authentication settings are in pg_hba.conf. Find its location:
sudo find / -name pg_hba.conf
Typically, it’s in /etc/postgresql/16/main/pg_hba.conf or /var/lib/pgsql/16/data/pg_hba.conf. Edit it with sudo:
sudo nano /etc/postgresql/16/main/pg_hba.conf
Look for lines like:
local all all peer
host all all 127.0.0.1/32 scram-sha-256
Change peer to md5 for password authentication on local connections. For remote access, add a line:
host all all 0.0.0.0/0 md5
Save and exit. Then restart PostgreSQL:
sudo systemctl restart postgresql
Enabling Remote Connections
To allow remote connections, edit postgresql.conf. Find it in the same directory as pg_hba.conf. Look for the line:
#listen_addresses = 'localhost'
Change it to:
listen_addresses = '*'
This listens on all network interfaces. For security, consider using a specific IP address instead. Restart PostgreSQL after making changes.
Verifying The Installation
Check that PostgreSQL is working correctly. Connect to the default database:
sudo -u postgres psql -c "SELECT version();"
You should see the PostgreSQL version number. Also check the service status:
sudo systemctl status postgresql
Test creating a table and inserting data:
sudo -u postgres psql -c "CREATE TABLE test (id SERIAL PRIMARY KEY, name VARCHAR(50));"
sudo -u postgres psql -c "INSERT INTO test (name) VALUES ('Hello World');"
sudo -u postgres psql -c "SELECT * FROM test;"
If you see the data, everything works. Remove the test table when done:
sudo -u postgres psql -c "DROP TABLE test;"
Common Issues And Troubleshooting
Sometimes things go wrong. Here are frequent problems and solutions.
Service Fails To Start
Check the logs:
sudo journalctl -u postgresql
Common causes include port conflicts or missing data directory. Ensure port 5432 is free:
sudo netstat -tulpn | grep 5432
If another service uses it, change PostgreSQL’s port in postgresql.conf.
Authentication Errors
If you get “Peer authentication failed,” check pg_hba.conf. Change peer to md5 for local connections. Then restart the service.
For remote connections, ensure the client IP is allowed in pg_hba.conf. Also check firewall settings:
sudo ufw allow 5432/tcp # For Ubuntu
sudo firewall-cmd --add-port=5432/tcp --permanent # For CentOS/Fedora
Package Conflicts
If you have multiple PostgreSQL versions, they may conflict. Use the official repository to install a specific version. Remove old versions with:
sudo apt remove postgresql-14 # For Ubuntu
sudo yum remove postgresql-server # For CentOS
Uninstalling PostgreSQL
To remove PostgreSQL completely, stop the service first:
sudo systemctl stop postgresql
sudo systemctl disable postgresql
Then remove the packages. For Ubuntu:
sudo apt purge postgresql postgresql-contrib
For CentOS:
sudo yum remove postgresql-server postgresql-contrib
Delete the data directory to free space:
sudo rm -rf /var/lib/postgresql # Ubuntu
sudo rm -rf /var/lib/pgsql # CentOS
Be careful—this removes all databases.
Frequently Asked Questions
Q: How do I check if PostgreSQL is installed on Linux?
Run psql --version or sudo systemctl status postgresql. If installed, you’ll see the version or service status.
Q: Can I install multiple PostgreSQL versions on the same Linux machine?
Yes, use the official repository and specify different ports. Each version runs on a separate port (e.g., 5432 for v16, 5433 for v15).
Q: What’s the default password for PostgreSQL on Linux?
There’s no default password. The postgres user uses peer authentication, which relies on the system user. Set a password with \password postgres in psql.
Q: How do I reset the PostgreSQL password on Linux?
Stop the service, edit pg_hba.conf to allow trust authentication, restart, then set a new password. Revert the config afterward.
Q: Is PostgreSQL better than MySQL for Linux?
It depends on your needs. PostgreSQL offers advanced features like full-text search and JSON support. MySQL is simpler for basic web apps. Both are excellent choices.
Conclusion
You now know how to install postgresql in linux using multiple methods. Whether you chose the default repository, official repo, or source compilation, the process is straightforward. Remember to configure authentication and enable remote access if needed. PostgreSQL is a robust database system that will serve you well for years. Test your setup with a simple database and start building your applications.
If you run into issues, refer to the troubleshooting section or check the official PostgreSQL documentation. The community is also helpful—forums and Stack Overflow have answers to most problems. Now go ahead and install PostgreSQL on your Linux machine. You’ll appreciate its power and flexibility.