Kritim Yantra
Jul 11, 2025
Imagine this: You're staring at your laptop, filled with excitement to build your first web application. You’ve heard great things about Laravel — how it's elegant, modern, and developer-friendly. You’ve also heard PostgreSQL is a rock-solid database system used by giants like Instagram and Reddit. But then, you pause...
"How do I even connect Laravel to PostgreSQL? And what’s CRUD anyway?"
Don’t worry — you’re not alone! If you’re a beginner, this tutorial is designed just for you. We'll walk through every step of setting up Laravel 12 with PostgreSQL and building a full CRUD (Create, Read, Update, Delete) system.
By the end, you'll have a simple app up and running, and most importantly — you’ll understand how it works.
Let’s get building! 🔨
Before diving in, make sure you have the following installed:
composer create-project laravel/laravel laravel12-postgres-crud
Navigate into your project:
cd laravel12-postgres-crud
Open the .env
file in your project and update the database section:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel_crud
DB_USERNAME=your_postgres_user
DB_PASSWORD=your_postgres_password
Create the database in PostgreSQL:
CREATE DATABASE laravel_crud;
👉 Tip: Use pgAdmin if you're more comfortable with a GUI!
Let’s say we’re building a Book Management App. Run this command:
php artisan make:model Book -mcr
This will generate:
Book.php
(Model)BookController.php
Open the migration file in database/migrations/xxxx_xx_xx_create_books_table.php
and edit it like this:
public function up(): void
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('author');
$table->text('description')->nullable();
$table->timestamps();
});
}
Run the migration:
php artisan migrate
👏 Now your books
table is ready in PostgreSQL!
In routes/web.php
, add:
use App\Http\Controllers\BookController;
Route::resource('books', BookController::class);
This creates RESTful routes like:
/books
(GET) → list books/books/create
(GET) → form to add book/books
(POST) → save new book/books/{id}
(GET) → show one book/books/{id}/edit
(GET) → edit book form/books/{id}
(PUT/PATCH) → update book/books/{id}
(DELETE) → delete bookCreate a new folder: resources/views/books
Here are some basic views:
<h1>All Books</h1>
<a href="{{ route('books.create') }}">➕ Add Book</a>
@foreach($books as $book)
<div>
<h3>{{ $book->title }} by {{ $book->author }}</h3>
<p>{{ $book->description }}</p>
<a href="{{ route('books.edit', $book) }}">✏️ Edit</a>
<form method="POST" action="{{ route('books.destroy', $book) }}">
@csrf
@method('DELETE')
<button type="submit">🗑 Delete</button>
</form>
</div>
@endforeach
(Repeat similarly for create.blade.php
and edit.blade.php
)
Here’s a basic BookController.php
:
use App\Models\Book;
use Illuminate\Http\Request;
public function index() {
$books = Book::all();
return view('books.index', compact('books'));
}
public function create() {
return view('books.create');
}
public function store(Request $request) {
Book::create($request->all());
return redirect()->route('books.index');
}
public function edit(Book $book) {
return view('books.edit', compact('book'));
}
public function update(Request $request, Book $book) {
$book->update($request->all());
return redirect()->route('books.index');
}
public function destroy(Book $book) {
$book->delete();
return redirect()->route('books.index');
}
Don't forget to allow mass assignment in Book.php
:
protected $fillable = ['title', 'author', 'description'];
Run the Laravel development server:
php artisan serve
Visit http://127.0.0.1:8000/books
— your CRUD app is now live!
🎉 You did it! From project setup to full PostgreSQL integration, you're now managing data in a real web app.
Now it’s time to customize this app — maybe add more fields, user authentication, or even use Bootstrap for styling. Every step you take builds confidence and skills!
💬 What will you build next with Laravel? Share your ideas in the comments below!
Q1: Why choose PostgreSQL over MySQL?
A: PostgreSQL offers advanced features (like JSONB, full-text search, etc.) and better compliance with SQL standards.
Q2: What is a migration in Laravel?
A: A migration is a version control system for your database. It allows you to define and modify your DB schema using PHP.
Q3: Can I use this CRUD app as a REST API?
A: Absolutely! Just modify your controller methods to return JSON and remove the Blade views.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google