Wednesday, December 24, 2025

Set Up PostgreSQL locally in Ubuntu

Install PostgreSQL


 sudo apt update
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql

Basic Setup




# Create database and user
sudo -u postgres psql

# In PostgreSQL shell:
CREATE DATABASE myapp;
CREATE USER myuser WITH PASSWORD 'mypassword';
GRANT ALL PRIVILEGES ON DATABASE myapp TO myuser;
\q


Connect



# Command line
psql -h localhost -U myuser -d myapp

# Connection string
postgresql://myuser:mypassword@localhost:5432/myapp

Environment Variables



DATABASE_URL="postgresql://myuser:mypassword@localhost:5432/myapp"