99

I'm trying to connect with PostgreSQL database through Laravel in order to do a php artisan migrate but doesn't seem to be directed since it's reading the database name of MySQL.

Here are the commands from database.php:

'connections' => array(

    'sqlite' => array(
        'driver'   => 'sqlite',
        'database' => __DIR__.'/../database/production.sqlite',
        'prefix'   => '',
    ),

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'database',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

    'pgsql' => array(
        'driver'   => 'pgsql',
        'host'     => 'localhost',
        'database' => 'postgres',
        'username' => 'postgres',
        'password' => 'root',
        'charset'  => 'utf8',
        'prefix'   => '',
        'schema'   => 'public',
    ),

    'sqlsrv' => array(
        'driver'   => 'sqlsrv',
        'host'     => 'localhost',
        'database' => 'database',
        'username' => 'root',
        'password' => '',
        'prefix'   => '',
    ),

),

If I remove the MySQL paths I'll get:

[InvalidArgumentException]
Database [mysql] not configured.


EDIT: When trying to do php artisan migrate I get a 'PDOException: could not find driver'. I'm using WAMP and I'm in Win8.1. Using PostgreSQL as database.


EDIT: Have experimented a series of alternative solutions but I'm still ought to get this solved. The php.ini file was checked in Apache, WAMP (from php folder) and PostgreSQL. The extension_dir is correct as it being -> extension_dir = "c:/wamp/bin/php/php5.5.12/ext/"

The extension=pdo_pgsql.dll and extension=pgsql.dll are uncommented.

Done the PATH trick in the 'System Variables' and rebooted. No chance.

Thanks for the help so far.

These are my drivers php_pdo_driver.h & php_pdo.h from C:\Program Files (x86)\PostgreSQL\EnterpriseDB-ApachePHP\php\SDK\include\ext\pdo

Information from phpinfo:

PHP Version 5.5.12

Compiler MSVC11 (Visual C++ 2012) Configure Command cscript /nologo configure.js "--enable-snapshot-build" "--disable-isapi" "--enable-debug-pack" "--without-mssql" "--without-pdo-mssql" "--without-pi3web" "--with-pdo-oci=C:\php-sdk\oracle\x64\instantclient10\sdk,shared" "--with-oci8=C:\php-sdk\oracle\x64\instantclient10\sdk,shared" "--with-oci8-11g=C:\php-sdk\oracle\x64\instantclient11\sdk,shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet=shared" "--with-mcrypt=static" "--disable-static-analyze" "--with-pgo"

vbence
  • 20,084
  • 9
  • 69
  • 118
Hyperion
  • 1,073
  • 2
  • 8
  • 10
  • If database is the name of your database then you are using a reserved word as your database name – David Jones Aug 15 '14 at 15:32
  • 1
    My PostgreSQL database name is postgres as indicated in the code^ – Hyperion Aug 15 '14 at 15:51
  • Awesome, totally misread your post. Are you sure laravel is trying to connect to your postgres database as default? – David Jones Aug 15 '14 at 15:54
  • I don't think so :( it seems it's trying to connect to MySQL by default and I'd like to figure out how to. Guess the user sgt as figured it out. Thanks a lot, my friend. – Hyperion Aug 15 '14 at 16:06

22 Answers22

103

Be sure to configure the 'default' key in app/config/database.php

For postgres, this would be 'default' => 'postgres',

If you are receiving a [PDOException] could not find driver error, check to see if you have the correct PHP extensions installed. You need pdo_pgsql.so and pgsql.so installed and enabled. Instructions on how to do this vary between operating systems.

For Windows, the pgsql extensions should come pre-downloaded with the official PHP distribution. Just edit your php.ini and uncomment the lines extension=pdo_pgsql.so and extension=pgsql.so

Also, in php.ini, make sure extension_dir is set to the proper directory. It should be a folder called extensions or ext or similar inside your PHP install directory.

Finally, copy libpq.dll from C:\wamp\bin\php\php5.*\ into C:\wamp\bin\apache*\bin and restart all services through the WampServer interface.

If you still get the exception, you may need to add the postgres \bin directory to your PATH:

  1. System Properties -> Advanced tab -> Environment Variables
  2. In 'System variables' group on lower half of window, scroll through and find the PATH entry.
  3. Select it and click Edit
  4. At the end of the existing entry, put the full path to your postgres bin directory. The bin folder should be located in the root of your postgres installation directory.
  5. Restart any open command prompts, or to be certain, restart your computer.

This should hopefully resolve any problems. For more information see:

Siavosh
  • 2,314
  • 4
  • 26
  • 50
lainceline
  • 1,184
  • 1
  • 9
  • 6
  • Thank you very much for your help. Please check the edit part in my post. BTW, the default database path was my first issue that was taken care already, the PDO driver problem came right after. – Hyperion Aug 16 '14 at 13:58
  • 1
    The DLL files exist in the extensions folder right? Also, try uncommenting another DLL: `extension=php_pdo.dll` That's the main PDO driver that the database specific ones rely on. – lainceline Aug 16 '14 at 14:12
  • Yes they do exist in that folder. Don't have the `extension=php_pdo.dll` in the `ext` nor in the `php.ini` – Hyperion Aug 16 '14 at 14:22
  • Can you make a quick php script with `phpinfo();`? It'll give you the actual extension directory and php.ini that's being loaded. There might be a conflict going on. – lainceline Aug 16 '14 at 14:47
  • Sorry for the delay. I'll copy/paste the info in the 1st post. Thanks a lot. – Hyperion Aug 16 '14 at 18:37
  • Do you need the whole thing though or just specific parts of it? – Hyperion Aug 16 '14 at 18:42
  • Don't paste the whole thing. I'm curious about the `extension_dir` setting and the loaded configuration file. Also, you should have a list in there of all the extensions that are being loaded; are the `pgsql` ones showing up there? – lainceline Aug 16 '14 at 19:52
  • `extension_dir` -> `c:/wamp/bin/php/php5.5.12/ext/` It seems there's hardly any reference to pgsql. It stats the only PDO drivers enabled are from mysql and sqlite. Don't see reference to the extensions other than the extension_dir. Thank you. – Hyperion Aug 16 '14 at 20:31
  • What about the line that says which `php.ini` is being loaded? The last thing I can think of is that its loading a `php.ini` that doesn't have the extension lines in it. If that ends up being fine you might want to check out Laravel Homestead - it makes it very easy to get a development VirtualBox set up and working. – lainceline Aug 16 '14 at 22:25
  • Loaded Configuration File: `C:\wamp\bin\apache\apache2.4.9\bin\php.ini` ... checked and it has -> `extension_dir = "c:/wamp/bin/php/php5.5.12/ext/"` – Hyperion Aug 16 '14 at 22:41
  • Ok, I set up WampServer and postgres on my Windows machine to test this out. Figured it out - copy `libpq.dll` from `C:\wamp\bin\php\php5.5.12` into `C:\wamp\bin\apache\apache2.4.9\bin`, and restart all services through WampServer. The pgsql extensions will now load, and the driver exception should be resolved. – lainceline Aug 17 '14 at 02:38
  • Thanks for all the work my friend. Really. I'd give you reputation points but it seems I need to have over 15 myself. Your solution didn't work so I did a fresh installation of everything, cleared the registry and now it finally works!!! I thought I was gonna quit PostgreSQL and move to MySQL. Thanks a lot!!! – Hyperion Aug 17 '14 at 15:44
  • Note that, for those using Apache or other web server, one needs to restart Apache for the changes to take effect: `sudo systemctl restart httpd.service` – Mugoma J. Okomba Aug 31 '16 at 06:36
  • If one is using php-fpm they need to restart it, apart from restarting web server (nginx/apache) – Mugoma J. Okomba Jun 20 '17 at 21:34
  • `'default' => 'pgsql'` not `postgres` – FooBar Feb 24 '18 at 14:44
  • damn you libpq.dll, you get me every time. :D – Alexandre Martini Aug 20 '19 at 01:11
49

For PDOException: could not find driver for MySQL, and if it is Debian based OS,

sudo apt-get -y install php5-mysql
Nasif Md. Tanjim
  • 3,862
  • 4
  • 28
  • 38
42

For PHP 7 in Ubuntu you can also do:

sudo apt-get install php7.0-pgsql

So, now you can do not uncomment lines in php.ini

UPD: I have a same error, so problem was not in driver. I changed my database.ini, but every time I saw an error. And I change database config in .env and errors gone.

8

I realize this is an old question but I found it in a Google search so I'm going to go ahead and answer just in case someone else runs across this. I'm on a Mac and had the same issue, but solved it by using HomeBrew. Once you've got it installed, you can just run this command:

brew install php56-pdo-pgsql

And replace the 56 with whatever version of PHP you're using without the decimal point.

William Brawner
  • 1,470
  • 17
  • 15
8

This worked for me:

$ sudo apt-get install php-gd php-mysql
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Lucky
  • 105
  • 1
  • 3
7

I had the same issue. This is what worked for me.

There are 2 php.ini files:

  • C:\wamp\bin\apache\apache2.4.9\bin
  • C:\wamp\bin\php\php5.5.12

NOTE: This is using my version of PHP and Apache, change to what yours are.

The php.ini file located in the both folders is what you need to update, the extentions:

  • extension=php_pdo_pgsql.dll
  • extension=php_pgsql.dll

These are what you need to uncomment (remove the ; symbol).

Restart both Wamp and the Command Prompt.

Hopefully it will work for you :).

user3293860
  • 79
  • 1
  • 1
  • Finally! I was trying it for 2 hours on Windows 10 Wamp Server, and the problem was that Wamp's options affects only the php.ini file in the apache folder, but not in the php folder. Thanks a lot! – Borjovsky Sep 06 '19 at 15:32
4

I got this problem too. I have solved this issue already. If u are using Wamp then perform the following steps.

  1. Go to wamp64/www/bin/php/php* (where * is the php version you are using)
  2. Edit file php and uncomment this line by removing the semicolon:
;extension=pdo_pgsql to extension=pdo_pgsql
  1. Save and restart your Wamp server

If it does not work, please check your .env and config/database again.

kano
  • 5,626
  • 3
  • 33
  • 48
11Strong
  • 129
  • 6
3
sudo apt-get install php7.1-pgsql
Sujal Patel
  • 2,444
  • 1
  • 19
  • 38
3

I had the same error on PHP 7.3.7 docker with laravel:

This works for me

apt-get update && apt-get install -y libpq-dev && docker-php-ext-install pdo pgsql pdo_pgsql

This will install the pgsql and pdo_pgsql drivers.

Now run this command to uncomment the lines extension=pdo_pgsql.so and extension=pgsql.so from php.ini

sed -ri -e 's!;extension=pdo_pgsql!extension=pdo_pgsql!' $PHP_INI_DIR/php.ini

sed -ri -e 's!;extension=pgsql!extension=pgsql!' $PHP_INI_DIR/php.ini
Ash
  • 672
  • 6
  • 21
3

You should remove the comment before extension on your driver.

The driver came disactivated by default you should active by yourself. after that it will work well.

I hope this do.

EACUAMBA
  • 461
  • 4
  • 8
2

Solved after 3 hours... I am using WAMP (PHP 7.2.4), PostgreSQL 10, Laravel 5.6.29. Loaded PHP extensions (pgsql, pdo_pgsql) from Wampserver 3.3, then I was able to connect to the PostgreSQL server with a simple php testcode from the www directory. But $ php artisan migrate still returned

PDOException::("could not find driver")

I checked the shell (git bash for windows) with $ php --ini, that returned C:\wamp64\bin\php\php7.2.4\php.ini instead of c:\wamp64\bin\apache\apache2.4.33\bin\php.ini, loaded by WAMP So you have to uncomment pgsql and pdo_pgsql extensions also in C:\wamp64\bin\php\php7.2.4\php.ini, and then migrate will work ...

2

In Linux(Ubuntu) just install the driver like so:

sudo apt-get install php-pgsql

This will install the driver with using your php current version.

1

If you are using Wamp in Windows, you have to edit your php.ini file directly from the PHP folder which usually is C:\wamp64\bin\php\php8.0.13 and need to uncomment extensions by removing ; in front of these extensions:

  1. extension=pdo_pgsql
  2. extension=pgsql
  3. check your php path in environment variables.
  4. restart your wamp and restart your cmd or vs code if using vs code CMD
MatuDuke
  • 4,997
  • 1
  • 21
  • 26
0

For those wanting to use Postgresql on OpenSuse (and co), try the following:

zypper --no-refresh in php5-pgsql
JWL
  • 13,591
  • 7
  • 57
  • 63
0

In Windows 8 PC with Laragon 3.4.0 180809, I faced the same issue. It happened in my case because I updated Laragon and it added a new version of PHP. So in laragon/bin/php/ I actually had two directories:

  • php-7.1.20-Win32-VC14-x64
  • php-7.1.7-Win32-VC14-x64

I added 7.1.20 into my PATH variable. But in my Command Console, running php --ini was showing that the path actually was fetching from the older one: php-7.1.7-Win32-VC14-x64. So I deleted the old one (for safety, I put it in Recycle Bin). But Laragon failed to start after that.

So, in laragaon/etc/apache2/mod_php.conf, I changed the path to the latest PHP version. Then restarted Laragon and the issue is resolved.

Reference: How to add another PHP version - Laragon Forum

Mayeenul Islam
  • 4,532
  • 5
  • 49
  • 102
0

For Ubuntu like 20.04+ just install:

sudo apt install php-pgsql

Remember to install apache2 and php before. It'll put the drivers in the right path on installation.

Caio Ladislau
  • 1,257
  • 13
  • 25
0

If you're using Docker you can add the below code inside php/Dockerfile. It'll install pdo pgsql pdo_pgsql that are necessary for connecting PHP to PostgreSQL.

FROM php:8.0.3-fpm
RUN apt-get update

# Install useful tools
RUN apt-get -y install apt-utils nano wget dialog vim

# Install important libraries
RUN echo "\e[1;33mInstall important libraries\e[0m"
    RUN apt-get -y install --fix-missing \
    apt-utils \
    build-essential \
    git \
    curl \
    libcurl4 \
    libcurl4-openssl-dev \
    zlib1g-dev \
    libzip-dev \
    zip \
    libbz2-dev \
    locales \
    libmcrypt-dev \
    libicu-dev \
    libonig-dev \
    libxml2-dev

RUN echo "\e[1;33mInstall important docker dependencies\e[0m"
RUN docker-php-ext-install \
    exif \
    pcntl \
    bcmath \
    ctype \
    curl \
    iconv \
    xml \
    soap \
    pcntl \
    mbstring \
    tokenizer \
    bz2 \
    zip \
    intl

# Install Postgre PDO
RUN apt-get install -y libpq-dev \
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
    && docker-php-ext-install pdo pdo_pgsql pgsql
Hanie Asemi
  • 1,318
  • 2
  • 9
  • 23
-1

Old thread I know, but I lost a couple of hours of my life with this. You also need to set the DB info in the .env file. You don't have to specify the driver here because the default specified in database.php is used (I think). I was struggling because it had DB_CONNECTION=mysql in the .env file and I was using pgsql.

Mark D
  • 19
  • 1
  • 5
-1

Just run this command

sudo apt-get -y install php*-mysql
Sunil kumawat
  • 804
  • 8
  • 14
-2

This worked for me:

$ sudo apt-get -y install php5.6-pgsql
$ sudo service apache2 restart

I am working with 32bit Ubuntu 14

SiHa
  • 7,830
  • 13
  • 34
  • 43
Nouks
  • 1
-2

I see you are using Windows. I was not able to fix this with enabling any of the extensions that come with my Windows WAMP Server. I tried PDO_ODBC and others and even found the Microsoft Official PDO_SQLSRV.

The solution for me was to install the PDO_SQLSRV drivers from a 3rd party website. I found the drivers through http://robsphp.blogspot.nl/2012/06/unofficial-microsoft-sql-server-driver.html

I usually don't use DLLs from random websites, but I was desperate and this worked for me. Hoping it might save others numerous hours of frustration.

halfer
  • 19,824
  • 17
  • 99
  • 186
Neo
  • 11,078
  • 2
  • 68
  • 79
-4

Using Laravel V 5.5.39 with Php 7.1.12 is working fine, but later (newer) php versions cause the problem. So, change Php version and you will get the solution 100% .

Hashmat Waziri
  • 476
  • 9
  • 9