Installing PostgreSQL on Ubuntu
PostgreSQL (often shortened to “Postgres”) is a free, open-source relational database (a database that stores data in tables made of rows and columns). It is one of the most popular and trusted databases in the world, used by everything from tiny side projects to giant companies. In this guide you will install PostgreSQL on Ubuntu, start it as a background service, understand the special postgres user it creates, and connect to it for the very first time using the built-in command-line tool, psql.
When to use PostgreSQL
Postgres is a great default choice when your app needs to store structured data with clear relationships — users, orders, payments, comments, and so on. It is rock-solid, handles complex queries well, and supports advanced features (JSON columns, full-text search, geospatial data) that many other databases lack.
You might not reach for Postgres if you only need a simple in-memory cache (use Redis), or if your data is large, unstructured, and document-shaped (a NoSQL database like MongoDB may fit better). For most web applications, though, Postgres is the safe, boring, excellent default.
Update the package list first
Before installing anything on Ubuntu, refresh the list of available packages. apt is Ubuntu’s package manager (the tool that downloads and installs software).
sudo apt update
Tip: Running
sudo apt updatedoes not upgrade your installed software — it only refreshes the index of what is available. It is safe to run anytime and you should run it before every install.
Install PostgreSQL
Ubuntu ships PostgreSQL in its official repositories, so a single command installs both the database server and the client tools. The postgresql-contrib package adds useful extra utilities and extensions that you will likely want later.
sudo apt install -y postgresql postgresql-contrib
The -y flag automatically answers “yes” to the confirmation prompt so the install runs without stopping.
Output:
Reading package lists... Done
Building dependency tree... Done
The following NEW packages will be installed:
postgresql postgresql-16 postgresql-client-16 postgresql-contrib ...
Setting up postgresql-16 (16.3-0ubuntu0.24.04.1) ...
Creating new PostgreSQL cluster 16/main ...
update-alternatives: using /usr/share/postgresql/16/man/man1/postmaster.1.gz ...
The exact version number depends on your Ubuntu release. On Ubuntu 24.04 LTS you get PostgreSQL 16; on Ubuntu 22.04 LTS you get PostgreSQL 14. Both work the same way for everything in this guide.
Start and enable the service
A service (also called a daemon) is a program that runs quietly in the background. Ubuntu manages services with systemd, and you control it with the systemctl command. The install usually starts PostgreSQL automatically, but let’s make sure it is running and set it to start on boot.
sudo systemctl start postgresql
sudo systemctl enable postgresql
sudo systemctl status postgresql
startruns the service now.enablemakes it launch automatically every time the server reboots.statusshows whether it is healthy.
Output:
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
Active: active (exited) since Mon 2026-06-15 10:12:03 UTC; 5s ago
active (exited) is normal for the top-level postgresql service — it is a small wrapper that starts the real database cluster. To see the actual database process, check sudo systemctl status postgresql@16-main.
The postgres system user
When you install PostgreSQL, it creates a Linux user account on your server named postgres. It also creates a matching database superuser (admin) with the same name. This is the master account for your database.
By default, Postgres uses peer authentication for local connections. Peer authentication means: “let a Linux user connect to the database as the database user of the same name, without a password.” So the Linux user postgres is allowed to connect as the database user postgres — no password needed — because the operating system has already verified who they are when they logged in.
This is why you connect by switching to the postgres Linux user. The sudo -u postgres part runs the next command as the postgres user.
sudo -u postgres psql
psql is the interactive terminal for PostgreSQL — a text prompt where you type commands and SQL.
Output:
psql (16.3 (Ubuntu 16.3-0ubuntu0.24.04.1))
Type "help" for help.
postgres=#
The postgres=# prompt means you are now connected. The # indicates you are a superuser.
List databases and exit
Inside psql, commands that start with a backslash (\) are meta-commands — shortcuts built into the tool, not SQL. Two you will use constantly:
\l— list all databases on the server.\q— quit and return to your shell.
postgres=# \l
Output:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------------+-----------------------
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
The three default databases are postgres (a default landing database), and template0 / template1 (blueprints used to create new databases — leave them alone). Now exit:
postgres=# \q
This returns you to your normal shell prompt.
Verify the version
It is good practice to confirm exactly which version you installed. You can do this without even logging in:
psql --version
Output:
psql (PostgreSQL) 16.3 (Ubuntu 16.3-0ubuntu0.24.04.1)
Or check the running server’s version from inside psql:
sudo -u postgres psql -c "SELECT version();"
Output:
version
---------------------------------------------------------------------------------------------------
PostgreSQL 16.3 (Ubuntu 16.3-0ubuntu0.24.04.1) on x86_64-pc-linux-gnu, compiled by gcc ...
(1 row)
The -c flag runs a single SQL command and exits immediately, which is handy for scripts.
Where things live on Ubuntu
Knowing where Postgres keeps its files helps you find logs and config when something goes wrong.
| Path | What it holds |
|---|---|
/etc/postgresql/16/main/postgresql.conf | Main server configuration |
/etc/postgresql/16/main/pg_hba.conf | Authentication rules (who can connect, and how) |
/var/lib/postgresql/16/main/ | The actual database data files |
/var/log/postgresql/ | Server log files |
The pg_hba.conf file (the name stands for “host-based authentication”) is where peer authentication is configured. You will edit it later when you want to allow password logins or remote connections.
Best Practices
- Always run
sudo apt updatebefore installing, so you get the latest available package version. - Use
systemctl enable postgresqlso the database survives a reboot — a stopped database means a down app. - Never run your application as the
postgressuperuser. Create a dedicated, limited database user and database for each app. - Keep the default peer authentication for the local
postgresadmin account — it is more secure than a password that could leak. - Note your installed version (
psql --version); you need it to find the right/etc/postgresql/<version>/config paths. - Do not delete or modify
template0andtemplate1— Postgres uses them internally.