If you want to learn how to install PostgreSQL on Linux, you picked a great moment to do it. PostgreSQL just became the most-used database among professional developers, and for good reason. It’s rock-solid, open source, and it runs everything from tiny side projects to massive enterprise systems. In this guide I’ll walk you through installing PostgreSQL 18 on both major Linux families: Ubuntu and Debian, plus RHEL, Rocky Linux, and AlmaLinux. We’ll also cover first login, creating a database user, and opening up remote access safely.

I’ll be honest with you up front. I broke my first PostgreSQL setup in about five different ways before it clicked. So this guide includes the mistakes I made, the errors you’ll probably hit, and exactly how to fix them. Let’s get into it.
Quick Answer: Installing PostgreSQL on Linux
On Ubuntu/Debian, add the official PGDG APT repository, then run sudo apt install -y postgresql-18. The service auto-starts. On RHEL/Rocky/AlmaLinux, install the PGDG RPM, disable the built-in module (on versions 8 and 9), install postgresql18-server, then run initdb and start the service. Connect with sudo -u postgres psql.
Why PostgreSQL? The Database 55% of Developers Now Rely On
In the 2025 Stack Overflow Developer Survey, PostgreSQL hit 55.6% adoption among professional developers. That edged past MySQL at 39.6% for the first time ever. An 18-point gap is not a fluke. People are choosing it on purpose.
Get a VPS from as low as $11/year! WOW!
I remember the exact moment I got hooked. I was building a Django project years ago, and I’d always defaulted to MySQL because it was familiar. A friend nudged me to try PostgreSQL instead. Within a week I was blown away. Proper data types, real constraint checking, transactional DDL that didn’t leave my schema in a broken half-migrated state. It just felt like a database built by people who cared. I never really went back.
There’s another reason PostgreSQL is having a moment: pgvector. This extension turns Postgres into a legitimate vector database for AI and semantic search workloads. So instead of bolting on a separate, expensive vector store, you keep everything in one place. That alone is pulling a whole new wave of developers in.
“PostgreSQL delivers reliability, control and the ability to run modern workloads: AI-driven, edge-distributed or Kubernetes-orchestrated, without vendor lock-in or infrastructure dead ends.” — Dave Page, VP Engineering at pgEdge and longtime PostgreSQL community leader
PostgreSQL 18 is the current stable release as of mid-2026. We’ll install that version directly from the official project repos, because the versions your distro ships by default are almost always a release or two behind. By the end of this guide you’ll have a working install on Ubuntu/Debian or RHEL, a secured user account, and optional remote access locked down properly. If you’re weighing your options, it’s worth comparing against our MySQL installation guide too.
Prerequisites
Before we start, make sure you’ve got the basics covered. This guide works on a wide range of systems.
- A Linux system: Ubuntu 20.04/22.04/24.04, Debian 11/12, RHEL 8/9/10, Rocky Linux 8/9/10, or AlmaLinux 8/9/10
- sudo privileges: You need administrative access to install packages and edit config files
- Internet access: For downloading packages from the official PostgreSQL repository
That’s it. No need to compile anything from source. If you want to brush up on managing the service afterward, keep our systemctl guide handy. You’ll lean on those commands constantly.
Install PostgreSQL on Ubuntu and Debian
This is the smoother of the two install paths. Ubuntu and Debian handle a lot of the setup automatically, including initializing the database and starting the service for you.
Step 1: Add the Official PostgreSQL APT Repository
Here’s the thing most beginners miss. If you just run apt install postgresql, you’ll get whatever older version your distro packaged. To get PostgreSQL 18, you need the official PGDG (PostgreSQL Global Development Group) repository. The good news is they ship a script that sets it all up for you.
sudo apt install -y postgresql-common
sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
The script auto-detects your distro version and adds the correct repo. When it asks for confirmation, just hit Enter.
Step 2: Install PostgreSQL 18
Now install the actual database server. One command does it.
sudo apt install -y postgresql-18
On Debian and Ubuntu, this is where the magic happens. The package automatically initializes a database cluster and starts the service. You don’t have to run initdb manually like you do on RHEL. Nice and clean.
Step 3: Verify the Service Is Running
Always confirm the service actually came up before moving on. Trust, but verify.
sudo systemctl status postgresql
You want to see active (running) in green. If it’s there, you’re golden. If you want to go deeper on starting, stopping, and enabling services at boot, our systemctl guide covers the whole toolkit. You can skip ahead to the First Login section now, or read on for the RHEL instructions.
Install PostgreSQL on RHEL, Rocky Linux, and AlmaLinux
The Red Hat family takes a few more steps. There’s one step in particular that trips people up every single time, and I’ll flag it loudly when we get there. Stick with me.
Step 1: Add the PGDG Yum Repository
Just like on Ubuntu, we want the official repo rather than the distro default. Install the PGDG RPM. The example below targets EL 9. Swap EL-9 for EL-8 or EL-10 to match your system.
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Step 2: Disable the Built-In AppStream Module (RHEL/Rocky 8 and 9 Only)
This is the one. The step everybody forgets. On RHEL, Rocky, and AlmaLinux versions 8 and 9, the system ships its own PostgreSQL through an AppStream module. If you don’t disable it, dnf will quietly install that older packaged version instead of the PGDG one you actually want. No error, no warning, just the wrong version.
Don’t skip this on EL 8 and 9
Run this command to disable the conflicting module before you install anything: sudo dnf -qy module disable postgresql. Skip it and you’ll spend an hour wondering why you got version 13 instead of 18.
Good news for RHEL 10, Rocky 10, and AlmaLinux 10 users: you can skip this step entirely. On version 10, the PGDG packages take priority automatically, so there’s no module to disable. One less thing to worry about.
Step 3: Install PostgreSQL 18
Now grab the server and the contrib package, which includes a bunch of useful extensions.
sudo dnf install -y postgresql18-server postgresql18-contrib
Step 4: Initialize the Database and Start the Service
Unlike Ubuntu, RHEL does not initialize the database for you. You have to do it manually. This caught me off guard the first time I set up Postgres on a Rocky box. I kept trying to connect and nothing worked, because the cluster literally didn’t exist yet.
sudo /usr/pgsql-18/bin/postgresql-18-setup initdb
sudo systemctl enable --now postgresql-18
The first line creates the database cluster. The second enables the service at boot and starts it right now. If you plan to allow remote connections later, you’ll also need to open the firewall, which our firewalld on RHEL guide walks through.
First Login: Connect to PostgreSQL
Both install paths land you in the same place now. Let’s connect to the database for the first time. This section confused me more than anything else when I started, so let me clear it up.
Use the postgres System Account
During install, PostgreSQL creates two things that share a name: a Linux system user called postgres and a database role also called postgres. These are not the same thing, and mixing them up is the number one beginner trip-up. I distinctly remember staring at a “role does not exist” error on my first install, completely baffled, before the penny dropped.
To connect, you switch to the postgres system user and launch psql from there.
sudo -u postgres psql
You’ll land at the psql prompt, which looks like this:
postgres=#
That’s it. You’re inside PostgreSQL.
Set a Password for the postgres Role
By default the postgres role has no password. Set one now so the account is protected. From the psql prompt, run:
ALTER USER postgres WITH PASSWORD 'your_strong_password';
When you’re done, type \q and press Enter to exit psql. One important note: the postgres role is a superuser. You should never use it for your actual application connections. We’ll create a dedicated, limited user for that next.
Create a New Database and User
Best practice is simple. Every application gets its own database role with only the privileges it needs. Using the postgres superuser for your app is like running everything as root. It works right up until it bites you.
Connect as the postgres role (sudo -u postgres psql), then run these three statements. Change the names and password to fit your project.
CREATE USER appuser WITH PASSWORD 'secure_pass';
CREATE DATABASE myapp OWNER appuser;
GRANT ALL PRIVILEGES ON DATABASE myapp TO appuser;
Now test it. Exit psql and connect directly as your new user.
psql -U appuser -d myapp -h 127.0.0.1
Since PostgreSQL 14, the default authentication method is scram-sha-256. That’s a meaningful security upgrade over the older md5 method, and it’s what you’ll see referenced in your config files. Stick with it for any new setup.
Allow Remote Connections (Optional)
Out of the box, PostgreSQL only listens on localhost. That’s a sane, secure default. If you need other machines to connect, like an app server reaching a separate database server, you’ll edit two files and open a firewall port. Do this carefully.
Edit postgresql.conf
Find the listen_addresses line. It’s usually commented out and set to localhost. Change it so PostgreSQL listens on other interfaces.
listen_addresses = '*'
The config file lives at /etc/postgresql/18/main/postgresql.conf on Debian/Ubuntu, or /var/lib/pgsql/18/data/postgresql.conf on RHEL.
Update pg_hba.conf
This file controls who’s allowed to connect and how. Add a host entry scoped to a specific IP range. Please, and I cannot stress this enough, never use 0.0.0.0/0 in production. That opens your database to the entire internet.
host myapp appuser 192.168.1.0/24 scram-sha-256
For the full reference on entry formats and auth methods, the official pg_hba.conf documentation is excellent.
Open Port 5432 in Your Firewall
PostgreSQL listens on port 5432. Open it for the right machines only.
- Ubuntu/Debian:
sudo ufw allow 5432/tcp— see our configure UFW firewall guide - RHEL/Rocky:
sudo firewall-cmd --add-port=5432/tcp --permanentthen reload — see our firewalld on RHEL guide
Then restart PostgreSQL to apply your config changes:
sudo systemctl restart postgresql
Security tip: skip the open port entirely
Honestly, my preferred approach is to not expose 5432 publicly at all. Use an SSH tunnel instead. You get an encrypted connection and zero exposed database ports. It’s the safer move nine times out of ten.
Common Installation Errors and How to Fix Them
I promised you the mistakes, so here they are. These two errors account for the vast majority of “it won’t connect” headaches I’ve helped people debug over the years.
Error: ‘role does not exist’
You’ll see something like FATAL: role "yourname" does not exist. This happens when you run psql as your normal Linux user. PostgreSQL tries to match your Linux username to a database role, doesn’t find one, and gives up. The fix is to connect as the postgres role instead:
sudo -u postgres psql
Error: ‘connection refused’ or ‘could not connect to server’
This almost always means PostgreSQL is only listening on localhost, or the service isn’t running. If you’re connecting remotely, double-check your listen_addresses setting in postgresql.conf and restart the service. On RHEL, also make sure you actually ran initdb. If you see initdb: command not found, you skipped Step 4 in the RHEL section. Go back and run the setup command.
When you’re truly stuck, the logs will tell you what’s wrong. Check them with journalctl:
sudo journalctl -u postgresql --no-pager | tail -50
Reading service logs is a skill worth building. Our journalctl to check logs guide shows you how to filter and follow them like a pro.
What to Do Next
You’ve got a working, secured PostgreSQL install. Now the fun part. Here’s where I’d point you next.
- Connect from your app: Wire it up to Node.js on Linux with the
pglibrary, or use Python with psycopg2 or asyncpg. - Add AI capabilities: Install the pgvector extension and turn your database into a vector store for semantic search.
- Automate backups: Pipe
pg_dumpinto a scheduled cron job so you never lose data. - Harden the port: If 5432 is exposed at all, set up fail2ban to block brute-force attempts.
- Scale it up: Managing multiple servers? Automate the whole install with Ansible to automate PostgreSQL provisioning.
One more housekeeping note. If you ever run into permission weirdness around the data directory at /var/lib/postgresql, our guide on Linux file permissions will help you untangle ownership and access bits.
PostgreSQL rewarded every hour I put into learning it, and I think it’ll do the same for you. If you found this useful, dig into the rest of our Linux service management and database guides. Got a setup that’s misbehaving? Reach out and tell me what error you’re seeing. Half the fun of this community is helping each other get unstuck. Now go build something.




