Laravel 12 with PostgreSQL: A Complete Setup Guide (2025)
Thinking about switching from MySQL to PostgreSQL in your Laravel project? Great choice! 🎯
PostgreSQL (or Postgres) is a powerful, open-source relational database that works beautifully with Laravel. In this post, you’ll learn how to set up Laravel 12 with a PostgreSQL database, step by step.
Whether you're a beginner or transitioning from MySQL, this guide is for you.
🧠 Why Choose PostgreSQL with Laravel?
Before we dive in, here’s why developers love Postgres:
- ✅ Open-source and widely supported
- ✅ Handles complex queries and JSON better than MySQL
- ✅ Strong ACID compliance and indexing options
- ✅ Perfect for large-scale and modern Laravel applications
⚙️ Step 1: Install PostgreSQL
Make sure PostgreSQL is installed on your machine.
For macOS:
brew install postgresql
For Ubuntu/Linux:
sudo apt update
sudo apt install postgresql postgresql-contrib
For Windows:
Download from https://www.postgresql.org/download/
After installation, create a new user and database:
sudo -u postgres psql
CREATE USER laraveluser WITH PASSWORD 'secret';
CREATE DATABASE laraveldb OWNER laraveluser;
Then exit the psql shell:
\q
🛠️ Step 2: Create a New Laravel 12 Project
If you haven’t already, create a new Laravel project:
composer create-project laravel/laravel laravel-postgres-app
cd laravel-postgres-app
Or use Laravel Sail for a dockerized setup:
./vendor/bin/sail up
📝 Step 3: Configure .env for PostgreSQL
Open your Laravel project’s .env file and update the database settings:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laraveldb
DB_USERNAME=laraveluser
DB_PASSWORD=secret
Also, in config/database.php, make sure the pgsql driver is properly configured (it usually is by default):
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
🧪 Step 4: Test the Connection
To test that Laravel can connect to your PostgreSQL database:
Create a test migration:
php artisan make:migration create_books_tableIn the migration file:
Schema::create('books', function (Blueprint $table) { $table->id(); $table->string('title'); $table->string('author'); $table->timestamps(); });Run the migration:
php artisan migrate
If no errors show up — 🎉 You’re officially connected to PostgreSQL!
🚀 Step 5: Bonus – Seed Some Data
Let’s seed a few books to test further.
Create a factory:
php artisan make:factory BookFactory --model=Book
In BookFactory.php:
public function definition(): array
{
return [
'title' => fake()->sentence(),
'author' => fake()->name(),
];
}
Create a seeder:
php artisan make:seeder BookSeeder
In BookSeeder.php:
public function run(): void
{
\App\Models\Book::factory()->count(10)->create();
}
Then call it from DatabaseSeeder.php:
$this->call(BookSeeder::class);
Run it all:
php artisan db:seed
You should now see 10 books seeded into your PostgreSQL database.
🧠 Pro Tips
- ✅ Use pgAdmin or TablePlus to visually manage and inspect your PostgreSQL data.
- 🛑 Watch out for case sensitivity in Postgres (e.g.,
"User"and"user"are different). - 🌐 PostgreSQL supports JSON columns and powerful indexing — great for modern apps.
🔍 Common Errors & Fixes
| Error | Solution |
|---|---|
could not connect to server |
Make sure Postgres is running and check your port (5432) |
password authentication failed |
Double-check your DB credentials in .env |
relation "table" does not exist |
Run php artisan migrate to create the table |
✅ Recap: Laravel + PostgreSQL Setup
| Step | Action |
|---|---|
| 1 | Install PostgreSQL |
| 2 | Create DB and user |
| 3 | Configure Laravel .env for pgsql |
| 4 | Test migration to verify connection |
| 5 | Seed data for development |
🧪 Try It Yourself
Build a simple students table with name, email, and course, and seed it with 5 fake records using factories.
🎁 Bonus: Use PostgreSQL’s json column type to store optional metadata like hobbies or languages.
💬 FAQs
Q1: Is Laravel slower with PostgreSQL?
Not at all! Laravel works efficiently with both MySQL and PostgreSQL. PostgreSQL even handles complex data structures better.
Q2: Can I use PostgreSQL in Laravel Sail?
Yes, just modify the docker-compose.yml to use the postgres image and update your .env.
Q3: Can I connect to multiple databases?
Absolutely. Just define additional connections in config/database.php.
📣 Final Thoughts
Using PostgreSQL with Laravel 12 opens up a world of flexibility and scalability. Whether you're working with large datasets, need advanced features like JSONB, or just prefer Postgres over MySQL, Laravel makes the switch easy.
So go ahead — mix Laravel's elegance with PostgreSQL's power and build something amazing! 💻💪
❓What’s Next?
What project are you planning to build with Laravel and PostgreSQL?
Drop your idea in the comments and let’s share tips and feedback! 👇
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
Sign in to join the discussion.