Kritim Yantra
Jul 10, 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.
Before we dive in, here’s why developers love Postgres:
Make sure PostgreSQL is installed on your machine.
brew install postgresql
sudo apt update
sudo apt install postgresql postgresql-contrib
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
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
.env
for PostgreSQLOpen 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',
],
To test that Laravel can connect to your PostgreSQL database:
Create a test migration:
php artisan make:migration create_books_table
In 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!
Let’s seed a few books to test further.
php artisan make:factory BookFactory --model=Book
BookFactory.php
:public function definition(): array
{
return [
'title' => fake()->sentence(),
'author' => fake()->name(),
];
}
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.
"User"
and "user"
are different).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 |
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 |
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.
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
.
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 project are you planning to build with Laravel and PostgreSQL?
Drop your idea in the comments and let’s share tips and feedback! 👇
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google