2

I am trying to connect ec2'instance Postgres DB from my PC (windows) thru Python.

Error I am getting

Steps i have taken before this : I am running Amazon Linux AMI EC2 instance. inbound rules of the running instance

I have installed Postgres SQL following the below command :

sudo yum install postgresql postgresql-server postgresql-devel postgresql-contrib postgresql-docs

sudo vim /var/lib/pgsql9/data/pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             0.0.0.0/0                 trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

My pg_hba.conf file:

sudo vim /var/lib/pgsql9/data/postgresql.conf
listen_addresses = '*'          # what IP address(es) to listen on;
                                    # comma-separated list of addresses;
                                    # defaults to 'localhost'; use '*' for all
                                    # (change requires restart)
port = 5432                             # (change requires restart)


sudo su - postgres
psql -U postgres 
sudo service postgresql start
CREATE DATABASE hacker_news WITH OWNER postgres;

I am not sure where i am doing wrong for the connectin g the postgresSql db in in my running ec2 instance.

Any help /Directions appreciated.

Basically i want to connect to my postgresql db n my remote device so that i can insert data base into it by python.

Thank you .

falsetru
  • 357,413
  • 63
  • 732
  • 636
Mighty
  • 31
  • 2
  • Can you connect to it with psycopg2 if you run the code on the ec2 instance? – Jacques Kvam Jul 02 '17 at 02:40
  • @JacquesKvam No I am not able to connect it throws me `OperationalError: could not connect to server: 'Connection timed out (0x0000274C/10060) Is the server running on host "ec2-107-22-157-62.compute-1.amazonaws.com" (107.22.157.62) and accepting TCP/IP connections on port 5432?'` Please Correct me if i am giving the right parameters : `conn = psycopg2.connect(host='ec2-107-22-157-62.compute-1.amazonaws.com', user='postgres',database='hacker_news',sslmode='require')` – Mighty Jul 02 '17 at 04:05
  • Did you try using "localhost" instead of the public IP? Maybe this is dumb, but did you check if postgres is running on the machine? – Jacques Kvam Jul 02 '17 at 04:09
  • @JacquesKvam Np ! But i donot want to connect with my Localhost .. i want to connect my ec2 instance host – Mighty Jul 02 '17 at 04:11
  • @JacquesKvam Yes i am able to connect and get result on ec2 instance by running as localhost (inside ec2 instance) , but while conneccting from remote system .. i am stil getting the error .... – Mighty Jul 02 '17 at 05:10
  • @JacquesKvam Thanks it working .. I am able to connect now!! – Mighty Jul 02 '17 at 05:39

0 Answers0