PostgreSQL 18 Installation & Setup Guide
Target OS: Ubuntu / Debian PostgreSQL Version: 18 (latest stable) Audience: Developers (Next.js, Django, Laravel, NestJS, etc.)
Overview
This document describes how to install PostgreSQL 18 on Debian or Ubuntu using the official PostgreSQL APT repository, followed by basic configuration suitable for application development and production use.
Ubuntu/Debian default repositories may ship older PostgreSQL versions. Using the official PostgreSQL repository ensures access to the latest supported release and timely security updates.
Step 0: Add the Official PostgreSQL APT Repository
Required to install PostgreSQL 18
sudo apt install curl ca-certificates gnupg -y
curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc \
| sudo gpg --dearmor -o /usr/share/keyrings/postgresql.gpg
echo "deb [signed-by=/usr/share/keyrings/postgresql.gpg] \
http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \
| sudo tee /etc/apt/sources.list.d/pgdg.list
sudo apt update
Step 1: Update System Packages
sudo apt update
sudo apt upgrade -y
Step 2: Install PostgreSQL 18
Installs:
- PostgreSQL server
- PostgreSQL client
- Common extensions
sudo apt install postgresql-18 postgresql-client-18 postgresql-contrib -y
Step 3: Verify PostgreSQL Service
Check service status:
sudo systemctl status postgresql
If not running:
sudo systemctl start postgresql
sudo systemctl enable postgresql
Step 4: Switch to the postgres System User
PostgreSQL creates a system user named postgres.
sudo -i -u postgres
Step 5: Access PostgreSQL Shell (psql)
psql
Expected prompt:
postgres=#
Exit:
\q
Step 6: Set Password for postgres User
ALTER USER postgres PASSWORD 'strongpassword';
Step 7: Create a Database
CREATE DATABASE mydb;
Step 8: Create an Application User (Recommended)
Do not use the
postgresuser in applications.
CREATE USER myuser WITH PASSWORD 'mypassword';
Grant privileges:
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Step 9: Enable Password Authentication
Edit authentication configuration:
sudo nano /etc/postgresql/18/main/pg_hba.conf
Change:
local all all peer
To:
local all all md5
Restart PostgreSQL:
sudo systemctl restart postgresql
Step 10: Test Login
psql -U myuser -d mydb
Successful login confirms correct setup.
Version Verification
psql --version
Expected output:
psql (PostgreSQL) 18.x
Connection String Example
postgresql://myuser:mypassword@localhost:5432/mydb
Notes & Best Practices
- PostgreSQL 18 is the latest stable and actively supported release
- PostgreSQL 17, 16, 15, 14 are still supported
- PostgreSQL 13 and below are end-of-life and should not be used
- Always use a dedicated database user for applications
- For production, consider:
- SSL connections
- Automated backups
- Role-based permissions