Laravel 5 + PostgreSQL: "Database [postgres] not configured." Error - php

I'm trying to get started with Laravel + PostgreSQL and been following the database tutorial.
Unfortunately, after updating the database configuration file and running php artisan migrate, the following error appears:
[InvalidArgumentException]
Database [postgres] not configured.
What puzzles me is that I didn't specify the "postgres" database in the configuration, but another database I set through cPanel, say "example_database".
Here's some relevant parts of my /config/database.php configuration:
'default' => env('DB_CONNECTION', 'postgres')
And inside the connections array of the same file:
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'example_database'), // This seems to be ignored
'username' => env('DB_USERNAME', 'example_username'),
'password' => env('DB_PASSWORD', 'example_password'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public'
],
The actual database credentials I'm using are working perfectly on my SQL Workbench client, so this seems to be a Laravel config problem. Any ideas? I have searched around for at least an hour to no avail.

You have to enter your configuration in the .env file.
The configuration you made will only be loaded if they are not already defined in .env
You need to use pgsql instead of postgres.
DB_CONNECTION=pgsql
DB_HOST=localhost
DB_DATABASE=DB_NAME
DB_USERNAME=USER
DB_PASSWORD=PW

Laravel sometimes caches your configurations. If you run into this problem while everything looks alright try running
php artisan config:cache

I know this is an old question and it already has an answer; however, here is an small explanation why:
If you check your database.php in the config directory, you will see that you have few connections types, including pgsql. So, the key have to match to the DB_CONNECTION in .env file. You can definitely replace pqsql connection key with postgres, and it will work on the same way.
However, I would recommend replacing the value DB_CONNECTION, instead of modifying the config.
DB_CONNECTION=pgsql

Related

Setting custom path for sqlite db in Laravel

I want to use the sqlite db stored in "C:\Folder\my_database.sqlite" , what should i do? I want to use that sqlite db. Can anyone please suggest me a solution?
So i can give the path to sqlite in database.php below:
'sqlite' => [
'driver' => 'sqlite',
'database' => "path to my sqlite db",
'prefix' => '',
],
Also if that is done, then i can change my default connection to sqlite. Like:
DB::setDefaultConnection('sqlite');
Can anyone please suggest me a solution?
As the Laravel Documentation states:
After creating a new SQLite database using a command such as touch
database/database.sqlite, you can easily configure your environment
variables to point to this newly created database by using the
database's absolute path:
'sqlite' => [
'driver' => 'sqlite',
'database' => "C:\Folder\my_database.sqlite",
'prefix' => '',
],
To make it the default connection, just set this in your .env file:
DB_CONNECTION=sqlite
Open your .env file and set,
DB_CONNECTION=sqlite
DB_DATABASE=C:\Folder\my_database.sqlite
further more information you can see: https://laravel.com/docs/5.7/database
for set sqlite database you can add absoulate path like this in database.php file..
'sqlite' => [
'driver' => 'sqlite',
'database' => 'C:\Folder\my_database.sqlite',
'prefix' => '',
],
for set a default sqlite connection update this in your .env file:
DB_CONNECTION=sqlite
for production update line(number 16) in database.php
'default' => env('DB_CONNECTION', 'sqlite'),
and then run this command in terminal for remove old configration and create new configration file.
php artisan config:cache

Connect Laravel Framework 5.6.27 and PHP 7.1.9 to SQL Server 2014

I'm coding an app using Laravel 5.6.27, PHP 7.1.9 and SQL Server 2014 and I tried several solutions found online (most are lower in the version I am using) including from this site.
Basically here's what I have done so far,
Configured .env to this:
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydatabasename
DB_USERNAME=sa
DB_PASSWORD=mypassword
database.php to this:
line 16:
'default' => env('DB_CONNECTION', 'sqlsrv'),
line 70:
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
I also tried replacing those entries in line 70 with the actual credentials,still unsuccessful.
I also tried this extension both from SQLSRV52.EXE and SQLSRV43.EXE and from unofficial releases.
extension=php_sqlsrv_71_ts_x64.dll
extension=php_pdo_sqlsrv_71_ts_x64.dll
extension=php_pdo_sqlsrv_71_ts.dll
extension=php_sqlsrv_71_ts.dll
extension=php_pdo_sqlsrv_71_ts_x86.dll
extension=php_sqlsrv_71_nts_x86.dll
extension=php_pdo_sqlsrv_7_ts_x86.dll
extension=php_pdo_sqlsrv_71_ts_x64.dll
Here's the error i encountered when i run php artisan migrate:
Illuminate\Database\QueryException : could not find driver (SQL: select * from sysobjects where type = 'U' and name = migrations)
and when i run DB::connection()->getPdo();
$ php artisan tinker
Psy Shell v0.9.6 (PHP 7.1.9 — cli) by Justin Hileman
>>> DB::connection()->getPdo();
PDOException with message 'could not find driver'
>>>
Additionally, I'm not sure if with this error in login page "SQLSTATE[HY000]: [Microsoft][ODBC Driver 17 for SQL Server]Protocol error in TDS stream (SQL: select top 1 * from [users] where [email] = test1234#gmail.com)" means it was able to find the driver thru web login and just php artisan that can find the driver.
I hope someone can enlighten me on this.
Thank you very much!
Besides Stefan said, also use a view where you just put phpinfo().
This is just to check if you have installed pdo extension for sql server.
It should look like that:
If not, you should install that extension.
And also pay attention on the others parameters in .env file like DB_DATABASE, DB_USERNAME, etc because they take precedence over the values set in database.php
In .env file, you have the port 3306 (mysql). Should be 1433 for Sql.

Laravel seems to randomly use the default database.php configuration instead of .env ones

EDIT : Thanks to #PedroFaria99, clear config cache solved the problem, but if anybody want to bring an explanation about the randomness aspect feel free.
I have an issue with my laravel 5.5 local installation (production environnement isn't impacted). Here Laravel is used as an API and serves a client-sided VueJS application.
Sometimes (randomly), my laravel is returning 500 error to my client. It can happens on various routes, never the same one, after 1 to 10 successives HTTP request or not and when I check the storage
[2018-03-09 13:44:08] production.ERROR: PDOException: SQLSTATE[HY000] [1045] Access Denied for user: 'forge'#'#localhost' (password: NO) in [...] Illuminate\Database\Connectors\Connector.php:119
However, my .env file is settup, and my database.php is using env() with default parameters are "forge" and "localhost". So I tried to change this parameter to "test", and the next 500 errors was same but with "test" instead of "forge".
I'm very confused, since this error doesn't happen systematicly.
.env file
APP_ENV=local
APP_DEBUG=true
DB_HOST=localhost
DB_DATABASE=mydatabase
DB_USERNAME=root
DB_PASSWORD=
database.php
...
'default' => env('DB_CONNECTION', 'mysql'),
...
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'test'),
'password' => env('DB_PASSWORD', '')
...
Try
php artisan cache:clear
php artisan config:cache
It happens because laravel saves env values in its cache and if your config is not cached it will take the configuaration that are saved in the database.php file. You can not directly access env values on runtime.
I hate to revive a dead thread but after some soul searching I found the issue might just be php's lack of thread safety : https://github.com/laravel/framework/issues/28571 and the great writeup here : https://mattallan.me/posts/how-php-environment-variables-actually-work/

pdo_oci_handle_factory: Error while trying to retrieve text for error ORA-01804

I'm using Laravel 5.5, php 7.1.9, and oracle 10g
i don't know if i'm connected to the database or not and if yes, is the problem in my code ?
PDOException SQLSTATE[HY000]: pdo_oci_handle_factory: Error while
trying to retrieve text for error ORA-01804
(ext\pdo_oci\oci_driver.c:640)
oracledb.php
return [
'oracle' => [
'driver' => 'pdo',
'tns' => env('DB_TNS', 'XE'),
'host' => env('DB_HOST', 'GREEN-PC'),
'port' => env('DB_PORT', '1521'),
'database' => env('DB_DATABASE', 'XE'),
'username' => env('DB_USERNAME', 'esprit'),
'password' => env('DB_PASSWORD', 'esprit'),
'charset' => '',
'prefix' => '',
'quoting' => false,
],
];
in my controller
public function show()
{
$res = DB::table('esp_etudiant')
->where('id_et','1630242')
->get();
print_r($res);
}
First, try connecting as SYSDBA.
$ sqlplus / as sysdba
There are several causes for the ORA-01804 error:
Check to make sure the following paths are set correctly in your environment:
$LD_LIBRARY_PATH
$ORACLE_HOME
$NLS_LANG
Your oracle client may be missing some timezone dat files in the $ORACLE_HOME directory. You should have a separate ORACLE_HOME for each release of Oracle. If this is the case, then :
The first thing to do is to check if ORA_TZFILE is set and if it is,
remove it and restart the database and listener. Oracle will then try
to load the $ORACLE_HOME/oracore/zoneinfo/timezlrg.dat file.
If for some reason the file that Oracle wants to use is not available
then this is the cause of the problem.
If the file is present then an alternative reason for the error could
be that Oracle cannot load the file because of permission problems.
If all above is correct then it might be a .dat file that is corrupt.
Check that the permissions are set to allow reads (chmod 444) for the files in the OCCI library.

Laravel/MySQL database connection

I have an unusual problem with my Laravel 4.2 project connecting to MySQL database. This is my database.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database_name',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
I use XAMPP for Ubuntu.
First, I've created a database in phpMyAdmin. When I tried to run php artisan migrate, it gave me an error of unknown database 'database_name'. I thought it was silly since I've just created a database in phpMyAdmin. Then I tried creating a database through the command line and tried running the migration again. It worked. Tables were created under the database I specified when I run show tables in the command line. It seems to me that the Laravel project is connected to a different mysql server than the phpMyAdmin. Is that even possible? How can I solve this problem?
I've been trying to solve this for hours but still, no luck. Hope someone can help me with my problem.
I solved the problem by following the solution in the link provided by Bulk. I just have to run sudo /opt/lampp/bin/php artisan migrate instead of the plain php artisan migrate. With this, artisan will use the MySQL version provided by XAMPP.

Categories