DevOps
PostgreSQL on Ubuntu: Install with Apt and Open psql
A straight Ubuntu/apt path from refreshed indexes to a running PostgreSQL cluster—inspect packages, install server plus contrib, sign into psql as postgres, confirm version(), then bookmark conf and logs before hardening.

PostgreSQL is the open-source relational engine many teams reach for first. On Ubuntu you rarely compile anything—the distro mirrors expose sane defaults once apt indexes are current—yet skipping prep bites newcomers ("unable to locate package…"). Pair these basics with the same apt + systemd hygiene captured for web tiers and branch later into creating PostgreSQL roles plus locking remote listeners safely once networking enters scope.
⏱️ Estimated setup time: 10–15 minutes.
Refresh apt metadata before installing PostgreSQL
Package installs stall without fresh mirrors—tell apt what exists today:
sudo apt-get update
That refreshes the lists Debian-derived tooling compares against /etc/apt/sources.list.
Inspect packages with apt show
Reading metadata catches pinned majors early (postgresql, rather than postgresql-15). Peek before committing bandwidth:
sudo apt show postgresql
You’ll see the Ubuntu-maintained synopsis plus transitive hints (postgresql-contrib, -doc, extension stubs).
Install PostgreSQL server and contrib on Ubuntu
Bring in the server binary plus optional-but-useful contrib utilities (pgbench, extra extensions):
sudo apt install postgresql postgresql-contrib
systemd units ship enabled—no manual initdb dance unless you customize clusters.
Log into psql as the postgres system user
Ubuntu seeds a dedicated unix account named postgres; invoke psql under that identity:
sudo -u postgres psql
Expect postgres=#—peer authentication via local sockets explains why no password prompt appears yet.
Verify PostgreSQL with SELECT version()
Inside psql (not the shell) confirm what landed:
SELECT version();
Exit \q afterward.
Paths, ports, and logs worth remembering
| Artifact | Typical Ubuntu layout |
|---|---|
| Main settings | /etc/postgresql/<version>/main/postgresql.conf |
| Host auth rules | /etc/postgresql/<version>/main/pg_hba.conf |
| Data directory | /var/lib/postgresql/<version>/main |
| Listening port | 5432 unless you changed port |
| Primary log | /var/log/postgresql/postgresql-<version>-main.log |
Swap <version> for whatever SELECT version(); reports as the packaged major.
Tighten access: roles and pg_hba.conf
Solid habits once basics work:
- Rotate database passwords away from defaults before exposing TCP listeners.
- Edit
/etc/postgresql/<version>/main/pg_hba.confso only trusted subnets reachhostentries. - Apply principle of least privilege: application roles read/write specific schemas—not superuser.
sudo apt upgraderegularly—security patches ride distro cadence.
Troubleshoot with PostgreSQL logs
When installs fail or connections bounce, stream logs:
sudo tail -f /var/log/postgresql/postgresql-<version>-main.log
Replace <version> with your cluster folder name (ls /var/log/postgresql/ helps).
Frequently asked questions
How do I install PostgreSQL on Ubuntu with apt?
Run sudo apt-get update to refresh indexes, then sudo apt install postgresql postgresql-contrib (swap apt-get for apt if that is your habit). systemd normally enables and starts the cluster automatically afterward.
How do I log into PostgreSQL as the postgres user on Ubuntu?
Use sudo -u postgres psql from any sudo-capable account on the machine; this enters psql without a password prompt because it relies on peer authentication through local UNIX sockets.
Where are postgresql.conf and pg_hba.conf on Ubuntu?
Both files normally live under /etc/postgresql/<major-version>/main/ alongside SSL certificates and systemd snippets referenced by the package maintainer scripts.
How do I check my PostgreSQL version on Ubuntu?
Inside psql run SELECT version(); or outside psql run sudo -u postgres psql -c "SELECT version();" - either surfaces the engine major.minor.patch plus distro compiler hints.
What should I do if apt cannot find the postgresql package?
Ensure Universe repositories are enabled on Ubuntu derivatives, rerun sudo apt-get update, and inspect upstream Postgres APT archives only when you truly need a newer major version than your distro ships.
⏱️ Total setup time: about 15 minutes when mirrors cooperate.
You now know how PostgreSQL lands on Ubuntu via apt, where configs live, and which companion guides cover roles or exposure—wire automation next with Linux crontab scheduling if backups or vacuum scripts need cadence.
Written by Shashikant Dwivedi
Engineer, occasional writer, full-time noticer. Based in Prayagraj, India. New essays land roughly twice a month.
Keep reading
Adjacent essays.
The newsletter
New articles in your inbox.
Occasional articles on engineering, tooling, and software development practices. No marketing, no fluff — just the article, when it's ready.
Unsubscribe with one click. Your email never leaves the list.


