Using PostgreSQL With Laravel

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.

--

--

--

Backend Developer | SEO

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tony Ayeni

Tony Ayeni

Backend Developer | SEO

More from Medium

Nginx install on windows

Deploying Laravel to Heroku via Git (CD)

How To Install Old Version Laravel Using Composer

Use NGINX hosting static websites