Technical8 min read

How to Install PostgreSQL on Arch Linux with Pacman (Step-by-Step)

Short answer: pacman -S postgresql, initdb -D /var/lib/postgres/data, systemctl enable --now postgresql. Full Arch setup, client install, and the connection fixes that follow.

Updated October 3, 2026 (1w ago)

Short answer: three commands.

  1. sudo pacman -S postgresql — installs the server and the psql client.
  2. sudo -iu postgres initdb -D /var/lib/postgres/data — creates the data directory.
  3. sudo systemctl enable --now postgresql — starts it now and on every boot.

Arch ships no post-install hook, so unlike Ubuntu the cluster is not initialized for you. Skipping step 2 is why a fresh systemctl start postgresql dies with a socket error. The full walkthrough — locale flags, users, databases — follows.

How to Install and Set Up PostgreSQL on Arch Linux

Step 1: Update your system

sudo pacman -Syu

A partial upgrade breaks PostgreSQL's build against a newer glibc or openssl, so update the package database and the whole system before installing. On a rolling release, pacman -Sy without the -u is the classic way to end up with an unbootable package set.

Step 2: Install PostgreSQL

Install the server and client with Pacman:

sudo pacman -S postgresql

This pulls in postgresql (the server, initdb, and psql) and registers the postgresql.service systemd unit. Arch's package is unpatched upstream PostgreSQL, so the commands below are identical to any other Linux distribution from here on.

Step 3: Initialize the Database Cluster

Before using PostgreSQL, you need to set up the database cluster, which is a group of databases managed by a PostgreSQL server.

sudo -iu postgres
initdb --locale $LANG -E UTF8 -D '/var/lib/postgres/data'
  • --locale $LANG: Sets the language settings for the database.

  • -E UTF8: Makes sure the database uses UTF-8 encoding.

  • -D '/var/lib/postgres/data': Chooses where the data will be stored.

Step 4: Start and Enable the PostgreSQL Service

Now, start the PostgreSQL service and enable it to start on boot automatically:

sudo systemctl start postgresql
sudo systemctl enable postgresql

These commands start the PostgreSQL service, so make sure it starts automatically whenever your system boots.

Step 5: Set Up a PostgreSQL User

To manage databases and users, switch to the PostgreSQL user:

sudo -iu postgres

Create a new PostgreSQL user with:

createuser --interactive

You'll be prompted to enter the username and decide whether the new user should be a superuser.

Step 6: Create a Database

With your PostgreSQL user set up, you can now create a database:

createdb your-database-name

Step 7: Access the PostgreSQL Shell

To start working with your databases, access the PostgreSQL command-line interface (CLI) with:

psql

Or, connect directly to a specific database:

psql mydatabase

Step 8: Exit the PostgreSQL Shell

When you're done, you can exit the PostgreSQL shell by typing:

\q

Install Only the PostgreSQL Client

Arch ships the client tools in the same package as the server — there is no separate postgresql-client. Installing postgresql gives you psql, pg_dump, and pg_restore, which is everything needed to talk to a database on another machine. To use them against a remote server, install the package and simply skip initdb and the service: nothing needs to listen locally.

Local Authentication: peer, scram, and the initdb default

When initdb runs without authentication flags, PostgreSQL configures trust for local connections — meaning any OS user on the machine can connect as the postgres superuser without a password. On a throwaway VM that is harmless; on anything shared it is a hole. Choose the policy up front: this is an alternative to the command in Step 3 for a fresh install, not something to run afterward. A second initdb refuses to touch a directory that already holds a cluster, so decide before you initialize. To change authentication on an existing cluster, edit pg_hba.conf and reload instead.

sudo -iu postgres
initdb --locale $LANG -E UTF8 -D '/var/lib/postgres/data' \
  --auth-local=peer --auth-host=scram-sha-256

peer maps the OS user to the same-named database role over the Unix socket, and scram-sha-256 requires a password over TCP. The rules live in /var/lib/postgres/data/pg_hba.conf, and edits there take effect after sudo systemctl reload postgresql — a reload, not a restart.

Create a Role With a Password, Then Connect as It

createuser --interactive creates a role but no password, which is enough for peer auth on the local socket and not enough for a TCP connection. For an application role, set one explicitly:

sudo -iu postgres
psql -c "CREATE ROLE app LOGIN PASSWORD 'change-me';"
psql -c "CREATE DATABASE appdb OWNER app;"

Then connect over TCP the way a deployed app does:

psql -U app -d appdb -h 127.0.0.1

Passing -h 127.0.0.1 forces a TCP connection and password auth; omitting it takes the Unix socket and peer auth instead, which is why the same command can succeed as the postgres user and fail as anyone else. For what lives inside a connection string, the database URL parser splits one into credentials, host, and options in the browser.

Connect From Another Machine

By default the server listens only on the loopback interface, so a connection from another host is refused outright. Two settings change that — listen_addresses in postgresql.conf and a host line in pg_hba.conf:

# postgresql.conf
listen_addresses = '*'
 
# pg_hba.conf — allow only the network that actually needs it
host    all    all    192.168.1.0/24    scram-sha-256

A listen_addresses change needs a restart, not a reload: sudo systemctl restart postgresql. Open port 5432 only to that network in the firewall, and prefer an SSH tunnel over exposing PostgreSQL directly to the public internet.

The Connection Errors You Will Actually Hit

  • could not connect: No such file or directory — the server is not running, so psql cannot find the Unix socket. Run systemctl start postgresql, and confirm initdb completed.
  • FATAL: role "app" does not exist — psql defaulted to your OS username as the role, and no role by that name exists. Connect as a role that does exist first, for example psql -U postgres -d postgres, then create app. Passing -U app cannot help here — it selects the same missing role.
  • FATAL: database "appdb" does not exist — the database was never created. Run createdb appdb, or connect to the default postgres database.
  • password authentication failed — pg_hba.conf requires a password and the role has none, or the password is wrong. Set one with ALTER ROLE app PASSWORD '...';.
  • Connection refused — the server is reachable but not listening on that host or port. Check listen_addresses and the firewall.

Upgrading PostgreSQL on a Rolling Release

Arch takes a new PostgreSQL major version long before a fixed-release distribution does, and a major upgrade leaves the old data directory unreadable until it is migrated — the service refuses to start with a version mismatch. Do not delete the cluster to get past it. Install postgresql-old-upgrade, which provides the previous binaries, and follow the Arch wiki's pg_upgrade procedure to move the data into a freshly initialised cluster. Take a pg_dumpall backup first: unlike a minor upgrade, this one is not reversible from the package cache.

FAQ

Where does PostgreSQL store its data on Arch Linux?

The database cluster lives in /var/lib/postgres/data by default — that's the directory passed to initdb in Step 3. Back up that directory (or use pg_dump) before system upgrades.

How do I start PostgreSQL automatically on boot?

sudo systemctl enable postgresql creates the boot-time symlink, and sudo systemctl start postgresql starts it immediately. Check status with systemctl status postgresql.

Why do I get "could not connect: No such file or directory"?

That error means the server isn't running — psql can't find the Unix socket. Run systemctl start postgresql, and if it fails, make sure you ran initdb first: a fresh install has no data directory until you initialize the cluster.

How is PostgreSQL different on Arch compared to Ubuntu?

The packages and defaults are nearly identical, but Arch's rolling release means PostgreSQL major-version upgrades arrive sooner and require a manual pg_upgrade of your data directory — Arch won't migrate the cluster for you.

How do I install only the psql client on Arch?

Arch has no separate client package: postgresql contains psql, pg_dump, and pg_restore alongside the server. Install it and skip initdb and the service to use the client against a remote database.

Why does psql say "role ... does not exist"?

psql defaults to your OS username as the database role. If that role was never created, connect as an existing one first — psql -U postgres -d postgres — and then create the role you want.

How do I connect with a username and password?

Force a TCP connection with -h, for example psql -U app -d appdb -h 127.0.0.1. Peer authentication over the Unix socket uses your OS identity, so a password is only required on a TCP connection, or when pg_hba.conf demands one.

Related Reads

Continue reading