Tools
Tools: PostgreSQL on Arch Linux: From Zero to Running in 5 Minutes
2026-01-19
0 views
admin
Why This Guide Exists ## Prerequisites ## Step 1: Install PostgreSQL ## Step 2: Initialize the Database Cluster ## Step 3: Start and Enable PostgreSQL ## Step 4: Verify Installation ## Step 5: Create Your First Database and User ## Step 6: Configure Local Access (Optional) ## Common Issues and Fixes ## Issue 1: "Directory exists but is not empty" ## Issue 2: "Could not bind to address" or Service Fails to Start ## Issue 3: Permission Errors ## Issue 4: "Peer authentication failed" ## Connecting to Your Database ## From the command line: ## From your application: ## Useful PostgreSQL Commands ## Security Best Practices ## Backup and Restore ## Backup a database: ## Restore a database: ## Backup all databases: ## Uninstalling PostgreSQL ## Why Arch + PostgreSQL is Great ## Next Steps ## Conclusion Most PostgreSQL installation guides are either outdated or don't cover the gotchas specific to Arch Linux. This guide will get you from zero to a working PostgreSQL installation in under 5 minutes, with proper systemd integration and best practices baked in. Arch makes this stupidly simple: This installs the latest PostgreSQL version available in the official repositories. As of writing, that's PostgreSQL 16.x, but Arch keeps this current. Before you can start PostgreSQL, you need to initialize the data directory: What's happening here: You should see output like: Use systemd to manage PostgreSQL: What each command does: Check that PostgreSQL is running: Switch to the postgres user and access the PostgreSQL prompt: Now you're in the PostgreSQL shell. Create a user with your system username: Exit the PostgreSQL prompt: Then exit back to your normal user: By default, PostgreSQL on Arch is configured for local socket connections. If you want to connect via TCP/IP on localhost, you may need to adjust the configuration. Edit the PostgreSQL configuration: Find and uncomment this line (it should already be set correctly): Edit the authentication file: Add or modify this line for local password authentication: If you see this error during initdb: Check if another PostgreSQL instance is running: If you have a Snap PostgreSQL installation: Fix ownership and permissions: This means you're trying to connect as a user that doesn't exist in PostgreSQL. Either: Connection string format: Once you're in the psql shell: If you need to completely remove PostgreSQL: Now that you have PostgreSQL running, consider: Installing PostgreSQL on Arch is straightforward when you know the steps. The combination of Arch's simplicity and PostgreSQL's power makes for an excellent development environment. Got questions or run into issues? Drop them in the comments below! Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse COMMAND_BLOCK:
sudo pacman -S postgresql Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
sudo pacman -S postgresql COMMAND_BLOCK:
sudo pacman -S postgresql COMMAND_BLOCK:
sudo -iu postgres initdb -D /var/lib/postgres/data Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
sudo -iu postgres initdb -D /var/lib/postgres/data COMMAND_BLOCK:
sudo -iu postgres initdb -D /var/lib/postgres/data CODE_BLOCK:
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process. The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
...
Success. You can now start the database server using: pg_ctl -D /var/lib/postgres/data -l logfile start Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process. The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
...
Success. You can now start the database server using: pg_ctl -D /var/lib/postgres/data -l logfile start CODE_BLOCK:
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process. The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
...
Success. You can now start the database server using: pg_ctl -D /var/lib/postgres/data -l logfile start COMMAND_BLOCK:
sudo systemctl start postgresql
sudo systemctl enable postgresql Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
sudo systemctl start postgresql
sudo systemctl enable postgresql COMMAND_BLOCK:
sudo systemctl start postgresql
sudo systemctl enable postgresql COMMAND_BLOCK:
sudo systemctl status postgresql Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
sudo systemctl status postgresql COMMAND_BLOCK:
sudo systemctl status postgresql CODE_BLOCK:
● postgresql.service - PostgreSQL database server Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled) Active: active (running) since... Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
● postgresql.service - PostgreSQL database server Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled) Active: active (running) since... CODE_BLOCK:
● postgresql.service - PostgreSQL database server Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled) Active: active (running) since... CODE_BLOCK:
psql --version Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
psql --version CODE_BLOCK:
psql --version COMMAND_BLOCK:
sudo -iu postgres
psql Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
sudo -iu postgres
psql COMMAND_BLOCK:
sudo -iu postgres
psql CODE_BLOCK:
CREATE USER your_username WITH PASSWORD 'your_secure_password';
CREATE DATABASE your_database OWNER your_username;
GRANT ALL PRIVILEGES ON DATABASE your_database TO your_username; Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
CREATE USER your_username WITH PASSWORD 'your_secure_password';
CREATE DATABASE your_database OWNER your_username;
GRANT ALL PRIVILEGES ON DATABASE your_database TO your_username; CODE_BLOCK:
CREATE USER your_username WITH PASSWORD 'your_secure_password';
CREATE DATABASE your_database OWNER your_username;
GRANT ALL PRIVILEGES ON DATABASE your_database TO your_username; CODE_BLOCK:
\q Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
exit Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
sudo -u postgres nano /var/lib/postgres/data/postgresql.conf Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
sudo -u postgres nano /var/lib/postgres/data/postgresql.conf COMMAND_BLOCK:
sudo -u postgres nano /var/lib/postgres/data/postgresql.conf CODE_BLOCK:
listen_addresses = 'localhost' Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
listen_addresses = 'localhost' CODE_BLOCK:
listen_addresses = 'localhost' COMMAND_BLOCK:
sudo -u postgres nano /var/lib/postgres/data/pg_hba.conf Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
sudo -u postgres nano /var/lib/postgres/data/pg_hba.conf COMMAND_BLOCK:
sudo -u postgres nano /var/lib/postgres/data/pg_hba.conf COMMAND_BLOCK:
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256 Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256 COMMAND_BLOCK:
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256 COMMAND_BLOCK:
sudo systemctl restart postgresql Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
sudo systemctl restart postgresql COMMAND_BLOCK:
sudo systemctl restart postgresql COMMAND_BLOCK:
# Remove the existing data directory
sudo rm -rf /var/lib/postgres/data # Reinitialize
sudo -iu postgres initdb -D /var/lib/postgres/data Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
# Remove the existing data directory
sudo rm -rf /var/lib/postgres/data # Reinitialize
sudo -iu postgres initdb -D /var/lib/postgres/data COMMAND_BLOCK:
# Remove the existing data directory
sudo rm -rf /var/lib/postgres/data # Reinitialize
sudo -iu postgres initdb -D /var/lib/postgres/data CODE_BLOCK:
ps aux | grep postgres
sudo lsof -i :5432 Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
ps aux | grep postgres
sudo lsof -i :5432 CODE_BLOCK:
ps aux | grep postgres
sudo lsof -i :5432 COMMAND_BLOCK:
sudo snap stop postgresql
sudo snap remove postgresql
sudo systemctl start postgresql Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
sudo snap stop postgresql
sudo snap remove postgresql
sudo systemctl start postgresql COMMAND_BLOCK:
sudo snap stop postgresql
sudo snap remove postgresql
sudo systemctl start postgresql COMMAND_BLOCK:
sudo chown -R postgres:postgres /var/lib/postgres/data
sudo chmod 700 /var/lib/postgres/data
sudo systemctl restart postgresql Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
sudo chown -R postgres:postgres /var/lib/postgres/data
sudo chmod 700 /var/lib/postgres/data
sudo systemctl restart postgresql COMMAND_BLOCK:
sudo chown -R postgres:postgres /var/lib/postgres/data
sudo chmod 700 /var/lib/postgres/data
sudo systemctl restart postgresql COMMAND_BLOCK:
# As your user (if you created a matching PostgreSQL user)
psql -d your_database # As a specific user
psql -U your_username -d your_database # Remote connection (if configured)
psql -h localhost -p 5432 -U your_username -d your_database Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
# As your user (if you created a matching PostgreSQL user)
psql -d your_database # As a specific user
psql -U your_username -d your_database # Remote connection (if configured)
psql -h localhost -p 5432 -U your_username -d your_database COMMAND_BLOCK:
# As your user (if you created a matching PostgreSQL user)
psql -d your_database # As a specific user
psql -U your_username -d your_database # Remote connection (if configured)
psql -h localhost -p 5432 -U your_username -d your_database CODE_BLOCK:
postgresql://username:password@localhost:5432/database_name Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
postgresql://username:password@localhost:5432/database_name CODE_BLOCK:
postgresql://username:password@localhost:5432/database_name CODE_BLOCK:
require 'pg' conn = PG.connect( host: 'localhost', dbname: 'your_database', user: 'your_username', password: 'your_password'
) Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
require 'pg' conn = PG.connect( host: 'localhost', dbname: 'your_database', user: 'your_username', password: 'your_password'
) CODE_BLOCK:
require 'pg' conn = PG.connect( host: 'localhost', dbname: 'your_database', user: 'your_username', password: 'your_password'
) CODE_BLOCK:
import psycopg2 conn = psycopg2.connect( host="localhost", database="your_database", user="your_username", password="your_password"
) Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
import psycopg2 conn = psycopg2.connect( host="localhost", database="your_database", user="your_username", password="your_password"
) CODE_BLOCK:
import psycopg2 conn = psycopg2.connect( host="localhost", database="your_database", user="your_username", password="your_password"
) CODE_BLOCK:
\l -- List all databases
\c dbname -- Connect to a database
\dt -- List all tables in current database
\du -- List all users/roles
\q -- Quit psql
\? -- Show all psql commands
\h -- Show SQL command help Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
\l -- List all databases
\c dbname -- Connect to a database
\dt -- List all tables in current database
\du -- List all users/roles
\q -- Quit psql
\? -- Show all psql commands
\h -- Show SQL command help CODE_BLOCK:
\l -- List all databases
\c dbname -- Connect to a database
\dt -- List all tables in current database
\du -- List all users/roles
\q -- Quit psql
\? -- Show all psql commands
\h -- Show SQL command help COMMAND_BLOCK:
pg_dump -U your_username your_database > backup.sql Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
pg_dump -U your_username your_database > backup.sql COMMAND_BLOCK:
pg_dump -U your_username your_database > backup.sql CODE_BLOCK:
psql -U your_username your_database < backup.sql Enter fullscreen mode Exit fullscreen mode CODE_BLOCK:
psql -U your_username your_database < backup.sql CODE_BLOCK:
psql -U your_username your_database < backup.sql COMMAND_BLOCK:
sudo -u postgres pg_dumpall > all_databases.sql Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
sudo -u postgres pg_dumpall > all_databases.sql COMMAND_BLOCK:
sudo -u postgres pg_dumpall > all_databases.sql COMMAND_BLOCK:
# Stop the service
sudo systemctl stop postgresql
sudo systemctl disable postgresql # Remove the package
sudo pacman -R postgresql # Remove data (WARNING: This deletes all your databases)
sudo rm -rf /var/lib/postgres Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK:
# Stop the service
sudo systemctl stop postgresql
sudo systemctl disable postgresql # Remove the package
sudo pacman -R postgresql # Remove data (WARNING: This deletes all your databases)
sudo rm -rf /var/lib/postgres COMMAND_BLOCK:
# Stop the service
sudo systemctl stop postgresql
sudo systemctl disable postgresql # Remove the package
sudo pacman -R postgresql # Remove data (WARNING: This deletes all your databases)
sudo rm -rf /var/lib/postgres - Arch Linux (obviously)
- Sudo access
- Basic terminal knowledge - sudo -iu postgres - switches to the postgres user with a login shell
- initdb - creates a new PostgreSQL database cluster
- -D /var/lib/postgres/data - specifies the data directory location - start postgresql - starts the service immediately
- enable postgresql - ensures it starts automatically on boot - Create the PostgreSQL user matching your system username
- Specify a different user: psql -U postgres - Use strong passwords: Don't use 'password' or '123456'
- Don't expose to the internet: Keep listen_addresses = 'localhost' unless you know what you're doing
- Use least privilege: Create separate users for each application with only the permissions they need
- Keep PostgreSQL updated: Run sudo pacman -Syu regularly
- Back up your databases: Use pg_dump regularly - Always up-to-date: Arch's rolling release means you get the latest PostgreSQL features
- Clean integration: Systemd management works flawlessly
- Minimal bloat: No unnecessary packages or configurations
- AUR availability: Easy access to PostgreSQL extensions and tools
- Performance: Arch's optimizations benefit database workloads - Learning SQL: PostgreSQL has excellent documentation at postgresql.org
- Installing pgAdmin: A GUI tool for managing PostgreSQL
- Exploring extensions: PostGIS for spatial data, pg_stat_statements for query analysis
- Setting up automated backups: Use cron and pg_dump
- Tuning performance: Adjust postgresql.conf for your workload - Use pacman for installation
- Initialize with initdb before starting
- Manage with systemd
- Create users and databases through psql
- Keep it updated and backed up
how-totutorialguidedev.toailinuxsystemdservershellcrontcp/ipswitchpostgresqlssldatabase