Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sarathiscookie's avatar

Laravel migration with postgreSQL

Hello Please help me to create Laravel migration with postgreSQL. I searched in google for this, but didn't find a good solution. Anybody did Laravel migration with PostgreSQL? Please share link here. Thanks.

0 likes
9 replies
getvma's avatar

In your .env file add the following;

DB_CONNECTION=pgsql

Then all of the instructions that work for mysql should apply. Well most of them anyway.

sarathiscookie's avatar

@getvma Thanks I added the following in .env but when I try php artisan migrate command, I am getting "[PDOException] could not find driver".

'DB_PGSQL_HOST=localhost DB_CONNECTION=pgsql DB_PGSQL_DATABASE=mainpagedb DB_PGSQL_USERNAME=postgres DB_PGSQL_PASSWORD=postgres DB_PGSQL_SCHEMA=fes'

getvma's avatar
getvma
Best Answer
Level 49

Either rename or make a copy of the existing ".env.example" to ".env"

Note! The values contained in the .env file is prepared for the Homestead box and should work with little modification if at all. However, if not using the Homestead VM but a Wamp or Xamp for example, you will need to provide the values specific to your environment.

There you will have the variable names actually used through out the framework. For example;

# Add this to change driver, the default is mysql
DB_CONNECTION=pgsql  # or sqlite

# Add this if you are not using Homestead and you know the default pgsql port has not changed.
DB_PORT=5432 

# Change the values to suit your environment if not on Homestead
DB_HOST=127.0.0.1
DB_DATABASE=homestead  # or mainpagedb
DB_USERNAME=homestead     # or postgres 
DB_PASSWORD=secret   # or postgres

# For the Schema you can add the following 
DB_PGSQL_SCHEMA=fes

# then edit in 'config/database.php
'pgsql' => [
            'driver'   => 'pgsql',
            'host'     => env('DB_HOST', 'localhost'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset'  => 'utf8',
            'prefix'   => '',
        // Notice the following has been modified
            'schema'   => env('DB_PGSQL_SCHEMA','public'),
        ],

You can create new environment variables (Key=>value) pairs and then call them elsewhere with env('Key', 'some default if key not set')

The easiest way to get started with Laravel is to use the Homestead Virtual Machine. However, using sqlite is equally as flexible to start. You can later switch to a different provider. For sqlite, just create an empty 'database/database.sqlite' file in the folder indicated.

2 likes
sarathiscookie's avatar

@getvma I have followed your instruction but I got "[PDOException] could not find driver". I have enabled PDO extensions in php.ini and now everything is working fine. Thanks for the detailed explanation.

1 like
arnabmunshi's avatar

I am using postgress

My database.php setup

'default' => env('DB_CONNECTION', 'pgsql'),

'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            // 'schema' => 'public',
            // 'sslmode' => 'prefer',
            'schema'   => env('DB_PGSQL_SCHEMA','public'),
        ],

and my .env setup

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laraapp
DB_USERNAME=postgres
DB_PASSWORD=123

after that when I run the cmnt on cmd

php artisan migrate

it through the error

[Illuminate\Database\QueryException]
  could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = mi
  grations)



  [PDOException]
  could not find driver
mikevrind's avatar

could not find driver

Check if the PG driver is available on your PHP installation and install it when you see it isn't installed.

Please or to participate in this conversation.