Laravel 5 with Postgres SQL - php

I’m working on Laravel 5 with postgres as database. I’ve configured postgres 9.4 and pgAdmin III and these are working normally. When I try to run migrate it is giving me error:
[PDOException]
could not find driver
This is my database.php
'default' => 'pgsql',
'pgsql' => [ 'driver' => 'pgsql',
'host' => '127.0.0.1',
'database' => 'fms',
'username' => 'postgres',
'password' => 'root',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public', ],
Initially I though, it was due to configuration of postgres on windows 7 but I tried with plain php it works perfect
<?php
$host = "host=127.0.0.1";
$port = "port=5432";
$dbname = "dbname=fms";
$db = pg_connect( "$host $port $dbname user=postgres password=root" );
if(!$db){
echo "Error : Unable to open database\n";
} else {
echo "Opened database successfully\n";
}
?>
I’ve enabled php_pgsql and php_pdo_sql in wamp as well. I’m not sure how to fix this on laravel 5.

As you said you already choosed Default Database as Postgres SQL
'default' => 'pgsql',
It is a must that you need to uncomment the pdo and postgres shared object in your php configuration settings (php.ini)
i.e., You need to uncomment the following lines in your php.ini
extension=pdo_pgsql.so
extension=pgsql.so
Note :
Don't forget to stop and start your apache after doing this changes (or php-fpm if using that instead).

I had the same problem with Laravel-WAMP-PostgreSql driver not found exception. I even successfully established direct connection to Postgre as you have, but with no luck with the "php artisan migrate" command.
After long research I found out that there are multiple php.ini files in which "extension=php_pdo_pgsql.dll" and "extension=php_pgsql.dll" are comented out.
The solution is (of course) to uncoment the extensions in the following files:
~/wamp/bin/php/php5.5.*/php.ini
~/wamp/bin/php/php5.5.*/phpForApache
~/wamp/bin/php/php5.5.*/php.ini.install
~/wamp/bin/php/php5.5.*/php.ini-development
~/wamp/bin/php/php5.5.*/php.ini-production
and
~/wamp/bin/apache/apache2.4.9/php.ini
** You can leave off the "php.ini-development" and "php.ini-production" (don't need to uncoment these files).

Like #jeff said, this is probably caused by not setting DB_CONNECTION=pgsql in the .env-file. The .env-file has MySQL preconfigured, so you must edit this file.

You have to make DB related changes in
config/database.php
.env file
and other settings in
php.ini settings
If you are still getting error, then clear cache and config
php artisan cache:clear
php artisan config:clear
It should work now!

Run this command to easily 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
If you do not have the drivers already installed, or it gives error like unable to load dynamic library PGSQL run this:
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.

you gotta modify .env file , then go to config>database.php and change the default to pgsql, and also in all your models file you need to change $protected $connection= 'pgsql'.

I'm using PHP7.3 and this worked for me, I forgot this:
apt-get install php7.3-pgsql
Everything runs smoothly afterwards. Just use whatever version of PHP you are using.

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.

Cannot find save handler 'redis' - Ubuntu

I am running LAMPP on ubuntu with PHP 5.6.23.
I decided to store my sessions in the Redis and I installed it. I also installed Predis too.
As a searched in the web I changed my php.ini to :
session.save_handler = redis
session.save_path = "127.0.0.1:6379"
But when tried create a session I am taking this error:
Warning: session_start(): Cannot find save handler 'redis' - session startup failed in /path/to/the/Untitled.php
You can think that there is a problem in Redis but it's not. It's working properly I can set something and I can check it with Redis-CLI :
My redis 'set' PHP code is :
<?php
require "predis/autoload.php";
$redis = new Predis\Client([
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
]);
$redis->set('x', '42');
$redis->set('name','test');
?>
Results in telnet:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
MONITOR
+OK
KEYS *
+1471853424.389215 [0 127.0.0.1:36912] "KEYS" "*"
*2
$4
name
$1
x
Session php code:
<?php
session_start();
$count = isset($_SESSION['cont']) ? $_SESSION['cont'] : 1;
echo $count;
$_SESSION['cont'] = ++$count;
?>
It must increase the number every refresh but It just displays an error.
I re-Installed Redis again (v 3.2.3) but still no difference.
Is there any way to solve it?
Could LAMPP be the reason of problem?
EDIT:
I started to use phpredis instead of predis.
You try different things. Predis is a direct client to your Redis Server. If you want to use them as your session handler you have to install the php module.
sudo apt-get install php5-redis
or for PHP7
sudo apt-get install php7.0-redis
Then "redis" should be available as session handler.

Use odbc eloquent outside laravel PHP7

I am using eloquent outside laravel. I have my own PHP application.
This is what i am using https://github.com/illuminate/database
My config is as following
$settings3 = array(
'driver' => 'odbc',
'dsn' => "Driver={SQL Server};Server={serverName};Trusted_Connection=true;Database=telesur_mis;",
'username' => 'user',
'password' => 'user',);
$capsule->addConnection($settings3,'teleappframework');
After executing this code I am getting the following error
Fatal error: Uncaught InvalidArgumentException: Unsupported driver [odbc]
I have PDO ODBC installed, i have also tested pdo odbc connection outside of eloquent.
The reason for using ODBC is, because i am using PHP7 and currently there is no PDO extension for SQL Server.
Can anyone help me on this?
i would download FreeTDS and just use the regular sqlsrvr driver it works like a charm (if you are on a unix env) there is a lot of posts on how to configure it etc just look it up.
PHP7 has a few modules disabled by default that were previously enabled in PHP5.
Most likely running php -m does not show the modules you need.
It's an easy fix though since the extension should already exist in the \ext\ folder that came with PHP7. You just need to modify your php.ini file to include the line:
extension=php_pdo_odbc.dll
extension=php_odbc.dll
Then from a command prompt or terminal test again using php -m to see that the module is now listed.

beanstalkd driver configuration error in laravel app

I am trying to use beanstalkd as the queue driver for my laravel app. I am getting this error when I am trying to push something on the queue.
Pheanstalk_Exception_ConnectionException
Socket error 111: Connection refused (connecting to localhost:11300)
Please note that in 'queue.php' I have just changed the default driver to 'beanstalkd' but haven't changed any settings for the driver. The settings for the driver are as follows-
'beanstalkd' => array(
'driver' => 'beanstalkd',
'host' => 'localhost',
'queue' => 'default',
),
Please help me with this.
Figured it out. Did these two things -
$ sudo vim /etc/default/beanstalkd
> START yes # uncomment
$ sudo service beanstalkd start
# Alternatively: /etc/init.d/beanstalkd start
Got it from this link - http://fideloper.com/ubuntu-beanstalkd-and-laravel4
The answer by #halkujabra is correct. The error is because beanstalkd is not running. To fix it you just have to start beanstalkd. For MacOS use this script https://gist.github.com/finger-berlin/1942295 to to that.
Download and save it as script.sh
Make it executable: chmod a+x script.sh
Run ./script.sh start

Connect Laravel on ubuntu to Azure SQL Server

I am developing an laravel app using ubuntu as OS and my database is in a remote Azure Server.
After long a research I'm about to give up. Installed freetds, php5-sybase etc etc.
here is my connection file: (the default is set to sqlsrv)
....
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'myhostname:myport',
'database' => 'mydatabasename',
'username' => 'myusername',
'password' => 'mypassword'
),
....
and the error that I am getting is this one:
PDOException
SQLSTATE[01002] Adaptive Server connection failed (severity 9)
Any sugestions? If you guys need more details please ask :)
Thanks in advance
I solved my problem. It was missing #domain in my query string example:
$pdo = new PDO("dblib:host=xxxx:1433;dbname=yyyy", 'username#domain', 'password');
I get solution in Read from the server failed when trying to connect to sql-azure from tsql
Edit /etc/freetds/freetds.conf file and use 8.0 TDS version.
If haven't this file install FreeTDS with
sudo apt-get install freetds-bin
Check these:
port separator is "," on windows and ":" on linux/Mac. Since you have 1433, the default one, it is better probably not to use it at all
definitely test first from command line using one of these :
$ tsql -S sectionNameInFreetdsconf -U user -P pass
$ tsql -H hostname -p port -U user -P pass
locate freetds.conf on your disk. It is possible it exists in several places and tsql uses one while PHP used another one. Best is to symlink them into one common file and test on that. Note that a common place for that file is ~/.freetds.conf beside /etc/ or /usr/local/etc/
there should be a [global] section on your freetds.conf file. Put there these lines :
tds version = 8.0
text size = 20971520
client charset = UTF-8

Categories