How to Install and Set Up PostgreSQL on Arch Linux
Step 1: Update Your System
sudo pacman -SyuThis command updates your package database and your system.
Step 2: Install PostgreSQL
To install PostgreSQL, use the Pacman package manager:
sudo pacman -S postgresqlThis command will install the PostgreSQL server and client on your system.
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:
\qFAQ
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.
Related Reads
- How to Dual Boot Windows 11 and Arch Linux — set up Arch itself, step by step, before adding PostgreSQL