This post shows how to configure PHP 7.4 (or PHP 7.x, in general) php.ini to enable the PostgreSQL driver, which comes in handy when I need to work on a new version of PHP after a very long break (like months).
Why The Need For a New PHP Version?
I am developing this application on Windows using PHP 7.2, Laravel 7, Angular 8, and PostgreSQL in my spare time. Recently, I have not spent much time on the project because of work. As a result, the codes and settings have become outdated due to Angular 10 and the upcoming Laravel 8. I need to upgrade the codes, at least to the latest framework versions. Also, I keep forgetting how to configure PHP to enable the PostgreSQL driver.
Download Latest PHP 7.x zip Distribution
I am not using a LAMP or XAMP. Instead, I use a zip distribution of PHP 7.x. To download the zip distribution, head to https://windows.php.net/download/ and click either the link for Non-Thread Safe or Thread Safe (recommended) distribution. See PHP FAQs.
Then, extract the zip file to some local folder. Next, create a copy of php.ini-development and rename it to php.ini.
Modify PHP 7.4 php.ini To Enable PostgreSQL Driver
Once we have the php.ini file, we need to modify it. Uncomment the following lines.
1 2 3 | ;extension_dir = "ext" ;extension=pdo_pgsql ;extension=pgsql |
At this point, we have enabled the PostgreSQL driver in PHP 7.4. We can now test the database connectivity with Laravel, e.g., running artisan migrate. Assuming we have a PostgreSQL database running locally, we will get the following error without those changes in php.ini.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | C:\Users\karldev\Desktop\dev\prj\ws\new\ws>php artisan migrate Illuminate\Database\QueryException could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE') at C:\Users\karldev\Desktop\dev\prj\ws\new\ws\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671 667| // If an exception occurs when attempting to run a query, we'll format the error 668| // message to include the bindings with SQL, which will make this exception a 669| // lot more helpful to the developer instead of just the database's errors. 670| catch (Exception $e) { > 671| throw new QueryException( 672| $query, $this->prepareBindings($bindings), $e 673| ); 674| } 675| 1 C:\Users\karldev\Desktop\dev\prj\ws\new\ws\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70 PDOException::("could not find driver") 2 C:\Users\karldev\Desktop\dev\prj\ws\new\ws\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70 PDO::__construct("pgsql:host=127.0.0.1;dbname=mydb;port=5442;sslmode=prefer", "mydb", "EYQLIkQkYJjRaVsLT721", []) |
Before Running PHP 7.4, Configure the PATH Environment
Before we can run any PHP codes from the command-line window, we have to configure the environment variable PATH. We can set it up in the command-line window before running any PHP codes. Consider the following sample command.
1 | set PHP=%PHP%;C:\Users\karldev\Desktop\dev\apps\php-7.4.10-Win32-vc15-x64 |
I am using C:\Users\karldev\Desktop\dev\apps\php-7.4.10-Win32-vc15-x64 for this post.