Knowledge Base
0%
Beginner ~20 min sudo required

Set Up PostgreSQL with a Laravel Application

Install PostgreSQL, create a dedicated database user, configure Laravel to use it, and run migrations.

0 / 4 done

Prerequisites

  • Laravel application deployed
  • Ubuntu server
1
Step 1 Command

Install PostgreSQL

apt update
apt install postgresql postgresql-contrib php8.3-pgsql -y
systemctl enable postgresql
2
Step 2 Command

Create database and user

sudo -u postgres psql <<EOF
CREATE DATABASE laravel_db;
CREATE USER laravel_user WITH ENCRYPTED PASSWORD 'YOUR_STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON DATABASE laravel_db TO laravel_user;
\q
EOF
3
Step 3 Command

Update Laravel .env

# Edit .env and set:
# DB_CONNECTION=pgsql
# DB_HOST=127.0.0.1
# DB_PORT=5432
# DB_DATABASE=laravel_db
# DB_USERNAME=laravel_user
# DB_PASSWORD=YOUR_STRONG_PASSWORD
4
Step 4 Command

Run Laravel migrations

cd /var/www/html/app
php artisan migrate
php artisan config:cache