skip to Main Content

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();
}
?>

2

Answers


  1. Chosen as BEST ANSWER

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


  2. 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search