shobhit sharma
shobhit sharma

Reputation: 39

Amazon EC2 cannot connect to PostgreSQL database

I am connecting to my PostgreSQL database via PHP using a Public IP (IPv4) provided in EC2 instance. The PHP is running but it does not let me create a connection with the database.

postgresql.conf:

listen_addresses = '*'  

pg_hba.conf:

# IPv4 local connections:
host    all             all             0.0.0.0/0               trust
# IPv6 local connections:
host    all             all             ::/0                    trust

php file:

$host        = "host = IPREMOVED ";
$port        = "port = 5432";
$dbname      = "dbname = dbName";
$credentials = "user = CREDENTIALREMOVED password = CREDENTIALREMOVED";

$response = array();

$db = pg_connect("$host $port $dbname $credentials");
if(!$db){
    echo "Connection Not established\n";
}
else{
    echo "sample code";
    pg_close($db);
}

Upvotes: 0

Views: 804

Answers (1)

Vineeth
Vineeth

Reputation: 1032

First thing : Hope you have given the wrong credentials here, just looks like the real ones. If not, please do change the original db credentials first!

Before going with php have you verified the port connection using telnet or shell?

Please do the following and see if it connects. Otherwise it will just be security group rule in AWS.

telnet 13.126.184.172 5432

And it connects(will show the escape character thing), do also the db connection using psql commands.

Upvotes: 1

Related Questions