0

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);
}
2
  • "does not let you create a connection". Error message/behaviour description? Commented Jun 30, 2017 at 5:07
  • "Connection Not established" Commented Jun 30, 2017 at 5:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It states "could not open connection to the host, on port 5432", but I have added HTTP, PostgreSQL, SSH as inbound rule in my security group
Did you do proper configuration with postgres to map it to external IP. I see your configurations in the question, but just make sure you did it wrong. Also did you run psql connection commands in shell from the instance where php rests?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.