Install PostgreSQL on Linux

Install PostgreSQL locally on Linux with hands-on steps, troubleshooting, and guidance on what to explore next in the PostgreSQL Tutorial.

Focus: install postgresql locally on linux

Sponsored

You’ve written SQL in your head, maybe even in a web console, but the real power of PostgreSQL only appears when it runs on your machine. Without a local install, every query is a round trip through a hosted service, every EXPLAIN plan hides behind a cloud dashboard, and every experiment waits on network latency or a disappearing free tier. Installing PostgreSQL locally on Linux removes that friction — you get a full-featured database engine in seconds, ready for hands-on learning, prototyping, and debugging. This lesson walks you through the exact steps to make PostgreSQL a permanent part of your development toolbox, using your Linux distribution’s native package manager or the official PostgreSQL repository.

The problem this lesson solves

When you’re learning PostgreSQL, the biggest blocker is access. Cloud-hosted databases (like AWS RDS or Neon) are great for production, but they add overhead: you need an account, a credit card, and a network connection every time you want to test a query. Local development removes all that friction. You get instant feedback, full superuser access, and the freedom to break things without affecting anyone else.

But local installs have their own traps. Different Linux distributions (Ubuntu, Debian, Fedora, Arch) use different package managers and sometimes ship different PostgreSQL versions. A simple apt install postgresql might give you version 14 when you need 16. You might also run into permissions issues — the default postgres user can be confusing if you’ve never used it. This lesson exists to eliminate those roadblocks with clear, distribution-aware steps.

After this lesson, you’ll have a running PostgreSQL server on your machine, a working psql client, and a sample database you can use for the rest of this PostgreSQL Tutorial track.

Core concept / mental model

Install PostgreSQL locally on Linux means getting three pieces working together:

  1. The server (postgres) — the background process that stores and serves your data.
  2. The client (psql) — the command-line tool you use to type SQL and view results.
  3. The data directory (/var/lib/postgresql/<version>/main or similar) — where all your databases actually live on disk.

Think of it like setting up a local web server: you need the server process, a way to talk to it (a port, usually 5432), and a folder to store static files. PostgreSQL gives you a superuser role named postgres by default, which is like an admin account for the database. You’ll use that role to create your own user and database for day-to-day work.

Key terms to remember:

  • Cluster — a set of databases managed by one server process.
  • Role — a user or group that can connect to the database.
  • Service name — on Linux, PostgreSQL registers itself as a service (e.g., postgresql or postgresql@16-main), so you can start, stop, and check its status with systemctl.

The installation process is essentially: install the server package, initialize the data directory (if the package doesn’t do it automatically), start the service, and then create a user and database you can work with.

How it works step by step

The installation varies slightly by distribution, but the logical flow is always the same. Here’s a distribution-agnostic overview:

  1. Update your package lists — Get the latest package metadata so you install the most recent version available from your repos.
  2. Install the PostgreSQL server and client — Usually a single package like postgresql or postgresql-16.
  3. Start the service — Use systemctl (or your init system) to enable and start the server.
  4. Verify installation — Connect with psql using the postgres system user.
  5. Create your own role and database — Set a password for postgres (optional but recommended) and create a user that matches your Linux username to avoid permission friction.
  6. Test with a sample query — Run a simple SELECT version(); to confirm everything works.

Let’s make it concrete with the most common distribution: Ubuntu/Debian.

Pro tip: If you’re on Fedora, replace apt with dnf and use postgresql-server + postgresql. For Arch, use pacman -S postgresql and follow the post-install initdb step. I’ll show Ubuntu first, then cover variations.

Hands-on walkthrough

This walkthrough uses Ubuntu 22.04 LTS with PostgreSQL 16 (the current stable at time of writing). The commands work for Debian and other apt-based systems with minor tweaks.

Step 1: Update and install

Open a terminal and run:

sudo apt update
sudo apt install postgresql postgresql-client

This installs the server, the client, and the default configuration. The installer creates a postgres system user and initializes a data directory automatically.

Step 2: Start the service

On most systems, the service starts automatically, but verify with:

sudo systemctl status postgresql

You should see active (running) in the output. If it’s not running, start and enable it for auto-start on boot:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Step 3: Connect with the postgres superuser

The default setup allows you to connect using the postgres system user via Unix sockets. Run:

sudo -u postgres psql

You’ll see a prompt like postgres=# — this is the SQL shell. Type \q to quit, or keep it for the next step.

Step 4: Set a password (recommended)

While connected as postgres, set a strong password for local connection security:

ALTER USER postgres WITH PASSWORD 'your_strong_password';

Then quit with \q.

Step 5: Create your user and a test database

It’s best practice not to use the postgres superuser for daily work. Create a role with your Linux username (so you can connect without sudo), and a database for practice:

sudo -u postgres createuser --interactive
# respond with your username and say y for superuser (or n for regular)

sudo -u postgres createdb mydb

Now connect directly with your own account:

psql -d mydb

You’re in! Run a quick sanity check:

SELECT version();

Expected output:

PostgreSQL 16.x on x86_64-pc-linux-gnu, compiled by gcc, ...
(1 row)

Pro tip: If psql isn’t found, the client package didn’t install the binary into your PATH. On Ubuntu, it lives at /usr/bin/psql — make sure your PATH includes that directory.

Compare options / when to choose what

The standard package manager approach works fine for most beginners, but you have alternatives depending on your needs. Here’s a quick comparison:

Method Pros Cons Best for
System package (apt/dnf) Simple, auto-configured, integrates with systemd Version may lag behind latest release Most beginners, general learning
Official PostgreSQL repository Latest version, community-built, precise control Requires extra setup steps, more moving parts Those who need specific versions or latest features
Docker container Isolated, reproducible, easy to wipe Adds Docker dependency, file system overhead Experimenting with configs, CI/CD

If you’re following this track on Linux, use the system package method for now. If you need a different version later, you can add the official PostgreSQL APT repository by downloading pgdg-repo from the PostgreSQL website and running sudo dpkg -i. Docker is a great fallback, but for local development, a native install gives you the full systemctl experience.

Read this: If your distribution’s package manager only offers PostgreSQL 14, don’t panic. The essential skills you learn (SQL, EXPLAIN, transactions) carry over. Only version-specific features matter later, and you can upgrade when ready.

Troubleshooting & edge cases

You’ll hit a few common issues, and here’s how to fix them.

1. Service won’t start

Check logs with journalctl:

sudo journalctl -u postgresql@16-main --no-pager | tail -20

Common cause: a corrupted data directory or missing permissions. Usually the initial initdb didn’t complete — re-run sudo postgresql-setup --initdb (on Fedora) or reinstall the package.

2. psql: error: could not connect to server: Connection refused

That means the server isn’t running. Start it with sudo systemctl start postgresql. If it still fails, the port might be in use — check with sudo ss -tlnp | grep 5432.

3. psql: FATAL: role "ubuntu" does not exist

You forgot to create a role matching your Linux username. Run sudo -u postgres createuser --superuser $USER and try again.

4. I forgot my postgres password

Reset it by becoming the postgres system user:

sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'new_password';"

Because PostgreSQL uses peer authentication over Unix sockets, you can always authenticate as the postgres system user without a password.

5. I installed via the official repo and can’t find pg_ctl

The binaries might be in /usr/lib/postgresql/<version>/bin. Add that to your PATH or use the full path.

Nerd note: On distros using libpq, the psql version may match the client library, not the server version. That’s fine — the server version is what matters for SQL features.

What you learned & what's next

You’ve successfully installed PostgreSQL locally on Linux, started the service, created a user and database, and verified the installation with psql and SELECT version(). More importantly, you now understand the architecture: server, client, data directory, and roles. This is the foundation for every subsequent lesson in the PostgreSQL Tutorial.

Next, you’ll learn how to query your new database — writing SELECT statements, filtering with WHERE, and using EXPLAIN to understand how PostgreSQL executes your queries. That’s where the real insight begins. With your local install humming, every future lesson becomes a hands-on sandbox.

If you hit a wall, revisit the troubleshooting section — most issues are one-liners. Now go run psql -d mydb and try your first query: SELECT 'Hello, PostgreSQL!' AS welcome;. Good luck!

Practice recap

Now that PostgreSQL is installed, create a fresh database named practice and a user with your Linux username. Connect to it, run SELECT current_user, current_database(); to confirm. Then try creating a simple table and inserting a row to solidify the connection between roles, databases, and basic SQL.

Common mistakes

  • Forgetting to start the service — you install the packages but never run systemctl start postgresql, so psql can't connect.
  • Trying to connect as postgres without sudo -u — the postgres role only works via peer authentication from the system user; psql -U postgres fails with a password prompt you can't answer.
  • Using the wrong version of PostgreSQL — your package manager may offer an older version, but you try commands from the latest docs; check SELECT version(); to see what you have.
  • Not creating a role for your Linux username — you get role \"ubuntu\" does not exist and think the install is broken.

Variations

  1. Use the official PostgreSQL APT repository to install a specific version (e.g., 17) instead of relying on your distro's default package manager.
  2. Run PostgreSQL in Docker with docker run --name pg -e POSTGRES_PASSWORD=pass -d postgres:16 for effortless isolation and cleanup.
  3. Use pg_config to check where binaries are installed, which is handy when you've used a non-standard installation path.

Real-world use cases

  • A developer spins up a local PostgreSQL instance to test schema changes before pushing to a shared staging database.
  • A data analyst uses a local PostgreSQL install to import CSV files and practice complex joins for a reporting project.
  • A CI/CD pipeline runs PostgreSQL in a container on a Linux runner to execute integration tests against a real database engine.

Key takeaways

  • PostgreSQL on Linux is installed via package manager (apt/dnf/pacman) and typically includes the server, client, and service management.
  • The default postgres role is the superuser; log in with sudo -u postgres psql for administrative tasks.
  • Always create a dedicated role and database for your daily work to avoid permission headaches.
  • Start the service with systemctl start postgresql and verify with systemctl status postgresql.
  • Test your install with SELECT version(); to confirm everything works before moving on.
  • If you hit connection errors, check the service status first — most issues are a missing start.

Sponsored

Sponsored

Discussion

Questions, corrections, and tips help everyone reading this page.

0 comments

Add a comment

Shown publicly with your comment.

Be constructive · max 4,000 characters

No comments yet — start the thread.

Related tutorials, quizzes, and articles for this topic.