VOOZH about

URL: https://dev.to/ishanjarwal/installing-postgresql-on-arch-linux-practical-setup-guide-1hme

⇱ Installing PostgreSQL on Arch Linux | Practical Setup Guide - DEV Community


Installing postgres on windows/mac is pretty simple, you just follow the steps on the installation wizard and you get it working. You will also find lots of video tutorials and blogs regarding installation in windows/mac. 

But when it comes to linux (specifically arch), things get interesting. Since linux gives you more control, setting up anything on it can seem tricky at first glance but as you go through, you understand each and every step and its purpose. 

Before getting into it, understand this : Postgres is not like MongoDB which you initialize once through the CLI by providing the --dbpath flag, it is a system service. Meaning it will run on the system 24x7 (if we configure it that way) and different system users will be able to use it according to the access they have been granted.


First things first : Installation

Always update the packages first :

sudo pacman -Syu

Install PostgreSQL:

sudo pacman -S postgresql

Lets initialize a Database Cluster

PostgreSQL requires a data directory initialization. This is the directory which holds the actual data (one-time step unlike MongoDB) :

sudo -u postgres initdb -D /var/lib/postgres/data

Flags explained

-u postgres → run command as postgres system user initdb → creates system tables & internal structure -D → data directory location

Start & Enable Service

Start server:

sudo systemctl start postgresql

Enable on every boot :

sudo systemctl enable postgresql

Check status:

systemctl status postgresql

Enter psql Shell

What is psql ? It is a command line interface to interact with out postgres service, databases, relations etc.

Lets enter the psql shell with the user “postgres”. 

On installing postgres, a new user named “postgres” is generated with default privileges.

sudo -u postgres psql

Why no password?

  • Default auth configuration is the “peer” auth. Peer auth uses our OS user to login
  • OS user identity is trusted

Create Role for a Project

It is always a good practice to create a separate role for every major project. This way you follow modular principles.

Inside psql:

CREATE ROLE myapp WITH
 LOGIN
 PASSWORD 'devpass123'
 CREATEDB;

Meaning:

  • LOGIN → allows authentication
  • PASSWORD → required for apps/ORMs
  • CREATEDB → grant access to create databases

Create a Database

CREATE DATABASE myapp_db OWNER myapp;

Ownership = full control over that DB.

Some frequently used and useful psql Commands

List Databases

\l

List roles (users)

\du

List tables/relations

\d

Show current connection details

\conninfo

Show the active role

SELECT current_user;

Connection URL Format

To connect to the database, you will need a url (Yes this is the one you add in your environment variables)

General structure : postgresql://USER:PASSWORD@HOST:PORT/DATABASE

Example : postgresql://myapp:devpass123@localhost:5432/myapp_db

This url can be used by:

  • Prisma
  • ORMs
  • Drivers
  • CLI tools

Enable Password Authentication (Critical for Prisma)

Edit config:

sudo nano /var/lib/postgres/data/pg_hba.conf

Change these lines according to your needs :

local all all md5
host all all 127.0.0.1/32 md5
host all all ::1/128 md5

Replace md5 with one of the following :

  • trust → no password
  • peer → OS identity
  • md5 / scram-sha-256 → password-based login

Restart PostgreSQL

To apply the config changes :

sudo systemctl restart postgresql

Testing our Connection

psql "postgresql://myapp:devpass123@localhost:5432/myapp_db"

OR

psql -U myapp -d myapp_db -h localhost -W

Flags explained

-U → database role -d → database name -h → host (forces TCP) -W → force password prompt

Some Common Errors & Causes you might face

  • password authentication failed → wrong password
  • database does not exist → DB missing
  • role does not exist → user missing
  • could not connect to server → service stopped / wrong port

Again, remember

PostgreSQL:

  • Always-running service
  • Config-driven behavior
  • URL = connection info only

No manual server start like MongoDB.

Happy building 🚀