In this article, we’ll focus on how to set up a new Laravel application with PostgreSQL as the database.
Before we dive into the setup, let’s briefly understand what PostgreSQL and Laravel are.
What is PostgreSQL?
PostgreSQL is a powerful, open-source object-relational database management system (ORDBMS). It extends SQL (Structured Query Language) with advanced features designed to safely store and scale complex data workloads. PostgreSQL is often preferred for its reliability, support for JSON, and ability to handle large applications.
What is Laravel?
Laravel is a PHP web development framework built for web artisans. It can be used to build full-stack applications and APIs (Application Programming Interfaces) that integrate with JavaScript libraries such as React or Vue.
By default, Laravel supports multiple databases. Depending on your project requirements, you can choose any of the following:
- MySQL
- SQLite
- PostgreSQL
- SQL Server
Step-by-Step Setup Guide
-
Install PostgreSQL
Visit the official PostgreSQL website and download the version best suited for your operating system (Windows, macOS, or Linux).
During installation:
- Create a password for the default postgres user.
- Leave the default port as 5432.
- Ensure pgAdmin is installed (it comes bundled by default).
-
Enable PostgreSQL in PHP
Locate your php.ini file (for me, it’s inside Laragon). Search for these two lines and uncomment them (remove the leading ;):
extension=pgsql extension=pdo_pgsql
-
Create a Database in pgAdmin
- Open pgAdmin.
- Connect to the server (enter the password you set for the postgres user).
- Right-click on Databases → Create → Database.
- Name your database, e.g., laravel_db.
-
Create a New Laravel Application
Open your terminal and run the following:
cd C:\laragon\www composer create-project laravel/laravel laravel_pg
-
Configure the .env File
-
Run Database Migrations
In your terminal, navigate to the project folder and run:
php artisan migrate
If successful, you’ll see messages like:
Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table
-
Serve the Application
Finally, start the development server:
php artisan serve
Visit http://127.0.0.1:8000 in your browser to confirm your Laravel application is running with PostgreSQL.
Conclusion
You’ve successfully set up a Laravel application with PostgreSQL.
From here, you can begin building models, controllers, and APIs backed by PostgreSQL. In upcoming tutorials, we’ll explore how to configure relationships, authentication, and advanced PostgreSQL features inside Laravel.
Top comments (2)
Nice read Emmanuel....thanks a lot
thanks for reading through