DEV Community

Cover image for How to Set Up Laravel with PostgreSQL (Step-by-Step Guide)
Emmanuel sofuwa
Emmanuel sofuwa

Posted on

How to Set Up Laravel with PostgreSQL (Step-by-Step Guide)

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:

  1. MySQL
  2. SQLite
  3. PostgreSQL
  4. SQL Server

Step-by-Step Setup Guide

  1. 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).
  2. 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
    
  3. 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.
  4. Create a New Laravel Application

    Open your terminal and run the following:

    cd C:\laragon\www
    composer create-project laravel/laravel laravel_pg
    
    
  5. Configure the .env File

    Database Section, Laravel .env

  6. 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
    
    
  7. 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)

Collapse
 
alpha2chris14 profile image
Christian Onyeka

Nice read Emmanuel....thanks a lot

Collapse
 
emmanuel_sofs profile image
Emmanuel sofuwa

thanks for reading through