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

lutkabe's avatar

Wrong Database Connection in env('DATABASE_URL')

Hi everyone,

In my project the php artisan migrate is supposed to target a mysql database but it throws the following exception: PDOException::("could not find driver"). I used Tinker and all env settings are correct except the env('DATABASE_URL) which is pointing to a pgsql connection string that I do not know where it comes from. I tried the php artisan cache:clear and php artisan config:clear commands but no luck. Would someone please let me know how to find and get rid of the unwanted mysterious pgsql connection string above? Thanks!

0 likes
21 replies
lutkabe's avatar

Already did. No hardcoded value, just 'url' => env('DATABASE_URL').

rovshena's avatar
  1. Check your .env file in root of your project.
  2. Check config/database.php
2 likes
lutkabe's avatar

No specific settings there in the .env and database.php.

jlrdw's avatar

Un-comment driver in php.ini. Restart server.

lutkabe's avatar

Checked this before too, the extensions extension=mysqli and extension=pdo_mysql were uncommented.

lutkabe's avatar

Not sure if this makes a difference or not, I am using Laragon as my dev environment. Thanks!

jlrdw's avatar

Show the database section from the env file, x out password. Also what is APP_URL set to?

lutkabe's avatar

Here is the my env database section:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=root
DB_PASSWORD=

and here is the APP_URL: APP_URL=http://localhost

jlrdw's avatar

Is localhost correct, and is mydb the correct database?

Also can you successfully connect to that database from the command line?

lutkabe's avatar

Both the localhost and mydb are correct. Using Tinker, I get the following error:

>>> DB::connection()->getPdo();
PDOException with message 'could not find driver'

I checked the PHP extension path and I can see the drivers exist.

Worth mentioning, using Tinker the env('DATABASE_URL') is:

>>> env('DATABASE_URL')
=> "postgres://postgres:[email protected]:5432/"

which confuses me. I do not have an active postgres database at all.

Looks like the DATABASE_URL has been set globally somewhere. Is there any global config that overrides the local config? I even set the DATABASE_URL to a bogus string in my project's .env file, cleared the cache and config, and restarted the server but still see the same postgres connection string above when query it from Tinker!

Thanks for taking time to looking into this!

jlrdw's avatar

Open database.php in config, comment out postgres whole section, run config:clear again.

Try without tinker.

lutkabe's avatar

Commented out the postgres whole section in the database.php, ran php artisan config:clear, and then php artisan migrate. It did not work and threw the same exception.

jlrdw's avatar

Grep the folders looking for a stray Global Connection. Also were you able to connect to mySQL from the command line. Not tinker the actual command line.

Laravel should be picking up the environment variables.

lutkabe's avatar

Grep returns billion results which I am still looking into them. How do you connect to mysql from the command line?

jlrdw's avatar

Command line admin: Change to the MySql bin folder:

c:\xampp\mysql\bin>mysql -uroot -pyour_password

Then to test you can:

show databases;
lutkabe's avatar

Yes, I can connect to the databases using the mysql cmdl. Besides, I have been using HeidiSQL to connect to my local MySQL databases forever.

jlrdw's avatar

Make sure your APP_URL in .env is correct.

lutkabe's avatar
lutkabe
OP
Best Answer
Level 1

I just commented out the 'url' => env('DATABASE_URL') in the database.php and it worked like a charm. Here is my updated 'mysql' config:

        'mysql' => [
            'driver' => 'mysql',
            // 'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

Thanks everyone!

jlrdw's avatar

That means APP_URL isn't correct, glad you figured it out.

renzocastillo's avatar

Hi @jlrdw I am having a similar issue. I am trying to connect using same configs as @lutkabe provided but it won't. BTW. -I am using an amazon RDS service. I can connect manually from this server to the remote database host .

  • This issue happened after switching to laravel 8

I get the following alert: SQLSTATE[HY000] [1045] Access denied for user 'user'@'999.999.99' (using password: YES) (SQL: select * from information_schema.tables where table_schema = mydb and table_name = migrations and table_type = 'BASE TABLE')

at vendor/laravel/framework/src/Illuminate/Database/Connection.php:692 688▕ // If an exception occurs when attempting to run a query, we'll format the error 689▕ // message to include the bindings with SQL, which will make this exception a 690▕ // lot more helpful to the developer instead of just the database's errors. 691▕ catch (Exception $e) { ➜ 692▕ throw new QueryException( 693▕ $query, $this->prepareBindings($bindings), $e 694▕ ); 695▕ } 696▕ }

Please or to participate in this conversation.