Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection refused to Postgresql

Tags:

Here is the code I wrote for connecting Postgresql using psycopg2. My psql and pgadminIII is also running.

import psycopg2

connection = psycopg2.connect(dbname="gps_heatmap",user="postgres",host="localhost",password="1234")
cursor = connection.cursor()

cursor.execute("DROP TABLE IF EXISTS roads")
cursor.execute("CREATE TABLE roads (" +
                   "id SERIAL PRIMARY KEY," +
                   "name VARCHAR," + 
                   "centerline GEOMETRY)")
cursor.execute("CREATE INDEX ON roads USING GIST(centerline)")

connection.commit()

But following error comes:

 OperationalError                          Traceback (most recent call last)
    <ipython-input-14-03e3f214b83e> in <module>()
          1 import psycopg2
          2 
    ----> 3 connection = psycopg2.connect(dbname="gps_heatmap",user="postgres",host="localhost",password="1234",port="5432")
          4 cursor = connection.cursor()
          5 

C:\Users\*******\Anaconda3\lib\site-packages\psycopg2\__init__.py in connect(dsn, database, user, password, host, port, connection_factory, cursor_factory, async, **kwargs)
    162                 for (k, v) in items])
    163 
--> 164     conn = _connect(dsn, connection_factory=connection_factory, async=async)
    165     if cursor_factory is not None:
    166         conn.cursor_factory = cursor_factory

OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

I edited the pg_hbf.conf as: host all all 0.0.0.0/0 md5

Again,same error repeated

like image 675
Himal Acharya Avatar asked May 23 '16 08:05

Himal Acharya


People also ask

Why is Postgres connection refused?

“Could not connect to server: Connection refused” To be sure that PostgreSQL is running, you can also restart it with systemctl restart postgresql. If this does not fix the problem, the most likely cause of this error is that PostgreSQL is not configured to allow TCP/IP connections.

Can't connect to PostgreSQL database?

First, double check that the Postgres process is running where you expect it to be. If you are trying to connect to a Postgres instance on the same host as your terminal, you can run lsof -p :5432 which will show which, if any, processes are listening on that port. The postgres process should be connected there.

Can't connect to server connection refused pgAdmin?

If pgAdmin displays this message, there are two possible reasons for this: the database server isn't running - simply start it. the server isn't configured to accept TCP/IP requests on the address shown.


1 Answers

Clarification

The answer below was accepted, however, it was not the solution to the problem... the problem was that the postgresql server was configured for port 5433, not the default port of 5432. This should fix the problem:

connection = psycopg2.connect(database="gps_heatmap", user="postgres", password="1234", host="localhost", port=5433)

Original answer

Try replacing dbname="gps_heatmap" with database="gps_heatmap" as the former is intended for use within a connection string and the latter when keyword arguments are passed to psycopg2.connect():

connection = psycopg2.connect(database="gps_heatmap", user="postgres", host="localhost", password="1234")

Or you could use a connection string:

connection = psycopg2.connect("dbname=gps_heatmap user=postgres host=localhost password=1234")
like image 131
mhawke Avatar answered Sep 28 '22 03:09

mhawke