Short answer: three commands.
sudo pacman -S postgresql— installs the server and thepsqlclient.sudo -iu postgres initdb -D /var/lib/postgres/data— creates the data directory.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 -SyuA 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 postgresqlThis 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 postgresqlThese 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 postgresCreate a new PostgreSQL user with:
createuser --interactiveYou'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-nameStep 7: Access the PostgreSQL Shell
To start working with your databases, access the PostgreSQL command-line interface (CLI) with:
psqlOr, connect directly to a specific database:
psql mydatabaseStep 8: Exit the PostgreSQL Shell
When you're done, you can exit the PostgreSQL shell by typing:
\qInstall 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-256peer 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.1Passing -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-256A 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, sopsqlcannot find the Unix socket. Runsystemctl start postgresql, and confirminitdbcompleted.FATAL: role "app" does not exist—psqldefaulted to your OS username as the role, and no role by that name exists. Connect as a role that does exist first, for examplepsql -U postgres -d postgres, then createapp. Passing-U appcannot help here — it selects the same missing role.FATAL: database "appdb" does not exist— the database was never created. Runcreatedb appdb, or connect to the defaultpostgresdatabase.password authentication failed—pg_hba.confrequires a password and the role has none, or the password is wrong. Set one withALTER ROLE app PASSWORD '...';.Connection refused— the server is reachable but not listening on that host or port. Checklisten_addressesand 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
- How to Dual Boot Windows 11 and Arch Linux — set up Arch itself, step by step, before adding PostgreSQL
- Your Database URL Is a Production Outage Waiting to Happen — the connection-string traps that bite after you deploy
- How to Build a Real-Time Chat App with WebSockets, JWT, and PostgreSQL — put the server to work in a full-stack project