Using PostgreSQL With Laravel

Tony Ayeni
1 min readMar 9, 2020
LARAVEL WITH POSTGRES

By default Laravel ships with MySQL. You may want to try out other SQL database type, changing that is very simple however there are some small configurations you have to make if your database of choice is postgres.

1. Installation

Download PostgreSQL to your pc from https://www.postgresql.org/download/

Or directly here (for windows) https://get.enterprisedb.com/postgresql/postgresql-12.1-3-windows-x64.exe

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

to (notice that “;” was removed)

extension=pdo_pgsqlextension=pgsql

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=pgsqlDB_HOST=127.0.0.1DB_PORT=5432DB_DATABASE=database_nameDB_USERNAME=postgresDB_PASSWORD=your_choosen_password

4. Migration

Create your migration files as usual and run migration.

ORM (Object-relational mapping) handles the underlining differences between different SQL databases (sqlite, MySQL, postgres), there is no need for any extra work as your migration command run successfully.

Hope this helps.

--

--