DEV Community

Daniel Muthoni
Daniel Muthoni

Posted on

Using PostgreSQL With Laravel

Using PostgreSQL With Laravel

In this article, I'll illustrate how to establish a connection between Laravel applications and a PostgreSQL database.

What advantages does PostgreSQL offer over the MySQL Database Engine?

MySQL is a relational database management system that lets you store data as tables with rows and columns. It’s a popular system that powers many web applications, dynamic websites, and embedded systems. PostgreSQL is an object-relational database management system that offers more features than MySQL. It gives you more flexibility in data types, scalability, concurrency, and data integrity.

1. Installation
Download PostgreSQL to your pc from

https://www.postgresql.org/download/

Then Install.

2. php.ini
MySQL is the default therefore there’s need to activate PostgreSQL for use with PHP on your pc by editing the php.ini

Search for these two lines within your php.ini and remove the “;” in front of each:

;extension=pdo_pgsql
;extension=pgsql
Enter fullscreen mode Exit fullscreen mode

to (notice that “;” was removed)

extension=pdo_pgsql
extension=pgsql
Enter fullscreen mode Exit fullscreen mode

3. .env
Change the following as appropriate within the Laravel’s .env file (all the defaults are ok except DB_DATABASE and DB_PASSWORD that you may have to change):

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=database_name
DB_USERNAME=postgres
DB_PASSWORD=your_choosen_password
Enter fullscreen mode Exit fullscreen mode

4. config/database.php

Go to Config/database.php and change the following lines.

'default' => env('DB_CONNECTION', 'pgsql'),
Enter fullscreen mode Exit fullscreen mode

4. Migration

Create your migration files as usual and run the migration.

Top comments (0)