1

I have installed lamp,phpmyadmin & postgresql in my ubuntu 20.04 version. below are the links: 1.https://www.linuxbabe.com/ubuntu/install-lamp-stack-ubuntu-20-04-server-desktop

2.https://www.linuxbabe.com/ubuntu/install-phpmyadmin-apache-lamp-ubuntu-20-04

3.https://tecadmin.net/how-to-install-postgresql-in-ubuntu-20-04

After,i did below operations

1.uncommented extension pgsql and pdo_pgsql in php.ini file

2.given trust method in pg_hba.conf file

3.GRANT ALL PRIVILEGES ON DATABASE my_db TO postgres;

I have insalled my database in pgadmin4 successfully. but i am unable to connect to it. below is my connection file. Getting access denied for user 'postgres'@'localhost' (using password: NO) error. I dont know what i am missing?

<?php
$host = "localhost";
$db_name = "my_db";
$db_username = "postgres";
$db_password = "";
try {
$dbConnection = new PDO("mysql:dbname=$db_name;host=$host;charset=utf8", "$db_username", "$db_password");
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo $e->getMessage();
}
?>
Olivier
  • 13,283
  • 1
  • 8
  • 24
konda
  • 185
  • 1
  • 12

2 Answers2

2

solved by running this command sudo apt-get install php-pgsql

konda
  • 185
  • 1
  • 12
0

Your put mysql in your connection string, which obviously won't work for PostgreSQL. You should use pgsql instead:

$dbConnection = new PDO("pgsql:dbname=$db_name;host=$host", $db_username, $db_password);
Olivier
  • 13,283
  • 1
  • 8
  • 24
  • yes, changed. getting could not find driver. enabled extension pdo_pgsql and pgsql in /etc/php/7.4/apache2/php.ini. still not working – konda Jan 29 '22 at 09:30