I can't connect to Postgresql with Laravel - php

I'm having trouble using Postgresql 12.3 with Laravel 7.24.0
Here's the error message I get:
Illuminate\Database\QueryException
could not find driver (SQL: insert into "posts" ("title", "content", "updated_at", "created_at") values (aze, bcb, 2020-08-11 18:02:26, 2020-08-11 18:02:26) returning "id")
Here's my setup (I tested the settings (host, port, username, password, and db_name) with DBeaver and with the command line):
.env :
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=<db_name>
DB_USERNAME=<user_name>
DB_PASSWORD=<password>
database.php:
'default' => env('DB_CONNECTION', 'pgsql'),
...
'connections' => [
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', '<db_name>'),
'username' => env('DB_USERNAME', '<user_name>'),
'password' => env('DB_PASSWORD', '<password>'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
],
And here's what I've tried:
composer require doctrine/dbal
Edit /etc/php/7.4/apache2/php.ini and uncommented extension=pdo_pgsql and extension=pgsql
apt-get install php-pgsql && sudo systemctl restart apache2
As the double quotes around the fields name and not the values confused me, I executed a raw SQL query to make sure the syntax was correct (I tested it with DBeaver).
$sqlQuery = "insert into posts
(title, post, updated_at, created_at) values
('aze', 'bcb', '2020-08-11 17:10:47', '2020-08-11 17:10:48') returning id";
$result = DB::select(DB::raw($sqlQuery));
I ran php artisan tinker, then DB::connection()->getPdo(); on command line and got this result:
=> Doctrine\DBAL\Driver\PDOConnection {#3085
inTransaction: false,
attributes: {
CASE: NATURAL,
ERRMODE: EXCEPTION,
PERSISTENT: false,
DRIVER_NAME: "pgsql",
SERVER_INFO: "PID: 27281; Client Encoding: UTF8; Is Superuser: off; Session Authorization: mespas; Date Style: ISO, DMY",
ORACLE_NULLS: NATURAL,
CLIENT_VERSION: "10.12 (Ubuntu 10.12-0ubuntu0.18.04.1)",
SERVER_VERSION: "12.3 (Ubuntu 12.3-1.pgdg18.04+1)",
STATEMENT_CLASS: [
"Doctrine\DBAL\Driver\PDOStatement",
[],
],
EMULATE_PREPARES: false,
CONNECTION_STATUS: "Connection OK; waiting to send.",
DEFAULT_FETCH_MODE: BOTH,
},
}
Which is apparently what what I'm supposed to get
I removed DATABASE_URL from database.php since it was not defined. I figure if all the other params are set, it is not necessary.
I constantly get the same message and after a few hours of research, I'm kind of out of ideas...

All right, so following #nibnut's advice I went and disabled php 7.2 for Apache and enabled 7.4
While I was at it, I checked the version for CLI and got this message:
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_pgsql' (tried: /usr/lib/php/20190902/pdo_pgsql (/usr/lib/php/20190902/pdo_pgsql: cannot open shared object file: No such file or directory), /usr/lib/php/20190902/pdo_pgsql.so (/usr/lib/php/20190902/pdo_pgsql.so: undefined symbol: pdo_parse_params)) in Unknown on line 0
For those interested, I found how to install it here.
It works now and my data are saved in the database, even though strangely enough I still get the message when running php -v
Thanks for the help #nibnut !

Related

PDOException with message 'could not find driver' Error for Laravel and Postgresql

First of all, the problem is about Laravel not postgresql or PHP. I can connect postgresql with a simple PHP file. But Laravel can't do it somehow.
When I try to connect postgresql server in my computer with laravel I get "PDOException with message 'could not find driver'" error. I am getting this error when I run DB::connection()->getPdo(); command at artisan tinker.
If I run php artisan migrate command, the error is 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')
My configuration is below:
Windows 10
Wamp Server 3.1.4
Apache 2.4.35
PHP 7.2.10
Laravel Framework 6.0.3
Related lines of Laravel .env file:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel_dnm1
DB_USERNAME=postgres
DB_PASSWORD=mrd.BE.265
Related lines of Laravel database.php file:
'default' => env('DB_CONNECTION', 'pgsql'),
...
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
When I run print_r(PDO::getAvailableDrivers()); at my server I get below output:
Array ( [0] => mysql [1] => pgsql [2] => sqlite )
Related lines of php info is below:
NOTE: There is no problem when I use mysql instead of postgresql.
NOTE2: I can connect the DB when I use regular PHP. Only Laravel gives this error.
install postgresql
sudo apt-get install php-pgsql
then uncomment pgsql and pdo-pgsql extensions in etc/php/$PHP_VERSION/apache2/php.ini file
then restart apache2
Verify your PHP version with php -v
Install php7.2-pgsql when needed.
I solved the issue. It is a bug at WAMP I think. When I edit php.ini from WAMP's menu it opens a different php.ini than active PHP version's. So I opened php.ini from file system and edited it from there.
There are different php version installed on your WAMP SERVER. when you click on WAMP icon it will show that pgsql and pdo_pgsql are active but for different php version. You need to check your php version using php -v command and then go to the WAMP folder and find that php version and edit php.ini file and enable pgsql extensions there.

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.

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 5.2 Mongo MonogDB Failed to parse MongoDB URI

I am getting this error:
MongoDB\Driver\Exception\InvalidArgumentException: Failed to parse MongoDB URI: 'mongodb://mongo:tcp://172.17.0.3:27017/mydatabase' in /var/www/laravel/vendor/mongodb/mongodb/src/Client.php:81
My enviroment variables look like this:
MONGO_DATABASE="mydatabase"
MONGO_HOST="mongo"
MONGO_PASSWORD=""
MONGO_PORT="27017"
MONGO_USERNAME=""
and my database.php looks like this
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_HOST', 'localhost'),
'port' => env('MONGO_PORT', 27017),
'database' => env('MONGO_DATABASE', 'mydatabase'),
'username' => env('MONGO_USERNAME', ''),
'password' => env('MONGO_PASSWORD', ''),
'options' => [
'database' => 'admin' // sets the authentication database required by mongo 3
]
I can correctly ping mongo from the container and it resolves to 172.17.0.3 as per the connection string. It seems as if the rest of the connection string is not getting generated correct?
I am running Laravel 5.2 and my package.json has this entry
"mongodb/mongodb": "^1.0.0",
"jenssegers/mongodb": "3.0.*",
As per https://github.com/jenssegers/laravel-mongodb 3.0.X is highest compatibility for Laravel 5.2.
Many thanks in advance
After further investigation my link from Amazon ECS was passing another MONGO_PORT which was overriding the env set in my .env file.

Oci8Exception in Oci8.php line 466

Attempting to connect to an Oracle database with yajra/laravel-oci8, below is the configuration. Note that I've also installed the instant client 11g corresponding to the bit version of Windows Server. I've ensured that the 11g ext is enabled in the php.ini.
Spent 4 days on this now, does anybody have any idea what's going on?
I don't even get an error message, just a stack dump which is attached.
'oracle' => [
'driver' => 'oracle',
'tns' => 'LISTENER',
'host' => '192.168.39.73',
'port' => env('DB_PORT', '1521'),
'database' => 'QORA',
'username' => 'ETEST',
'password' => 'ETEST',
'charset' => env('DB_CHARSET', 'AL32UTF8'),
'prefix' => env('DB_PREFIX', ''),
'prefix_schema' => env('DB_SCHEMA_PREFIX', ''),
],
Your error messages are showing you are trying to connect on port 3306, which is the standard MySQL port. According to your config, you're looking at the DB_PORT value from the .env file, with a backup of 1521 if that doesn't exist.
My guess is that your .env file has the following line:
DB_PORT=3306
You either need to comment or remove that line, or change 3306 to 1521.

Categories