m3asmi
m3asmi

Reputation: 1262

how can I connect to a postgresql database from external psycopg2.connect?

I would like to connect to a postgresql database using python from a different server.
I triyed this :

conn_string = "host=192.168.1.1 dbname='"+db7+"' user='user' password='"+pw7+"'"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()

but I get the error:

    conn = psycopg2.connect(conn_string)
  File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
    connection_factory=connection_factory, async=async)
psycopg2.OperationalError: FATAL:  database "database" does not exist

Upvotes: 1

Views: 1133

Answers (1)

Dhannababu Nyros
Dhannababu Nyros

Reputation: 11

Remove unnecessary quotes in the syntax.

Follow this structure.

conn = psycopg2.connect(host = "localhost",database="ur_database_name", user="db_user", password="your_password")

Example.

conn = psycopg2.connect(host = "localhost",database="studentesdb", user="postgres", password="admin")

Upvotes: 1

Related Questions