Python psycopg2 timeout

When using the keyword arguments syntax to the connect function it is possible to use any of the libpd supported connection parameters. Among those there is connect_timeout in seconds: db = psycopg2.connect ( host=dhost, database=ddatabase, user=duser, password=dpassword, connect_timeout=3 ) http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS http://initd.org/psycopg/docs/module.html A connection time out raises an OperationalError exception.

Postgres SSL SYSCALL error: EOF detected with python and psycopg

The error: psycopg2.operationalerror: SSL SYSCALL error: EOF detected The setup: Airflow + Redshift + psycopg2 When: Queries take a long time to execute (more than 300 seconds). A socket timeout occurs in this instance. What solves this specific variant of the error is adding keepalive arguments to the connection string. keepalive_kwargs = { “keepalives”: 1, … Read more

query from postgresql using python as dictionary

Tnx a lot Andrey Shokhin , full answer is: #!/var/bin/python import psycopg2 import psycopg2.extras conn = psycopg2.connect(“dbname=uniart4_pr host=localhost user=user password=password”) cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) cur.execute (“select * from port”) ans =cur.fetchall() ans1 = [] for row in ans: ans1.append(dict(row)) print ans1 #actually it’s return

Getting the id of the last record inserted for Postgresql SERIAL KEY with Python

You might be able to use the RETURNING clause of the INSERT statement like this: result = conn.execute(“INSERT INTO user (name, country_id) VALUES (‘Homer’, 123) RETURNING *”) If you only want the resulting id: result = conn.execute(“INSERT INTO user (name, country_id) VALUES (‘Homer’, 123) RETURNING id”) [new_id] = result.fetchone()

Insert Python Dictionary using Psycopg2

from psycopg2.extensions import AsIs song = { ‘title’: ‘song 1’, ‘artist’: ‘artist 1’ } columns = song.keys() values = [song[column] for column in columns] insert_statement=”insert into song_table (%s) values %s” # cursor.execute(insert_statement, (AsIs(‘,’.join(columns)), tuple(values))) print cursor.mogrify(insert_statement, (AsIs(‘,’.join(columns)), tuple(values))) Prints: insert into song_table (artist,title) values (‘artist 1’, ‘song 1’) Psycopg adapts a tuple to a record … Read more

Execute .sql schema in psycopg2 in Python

You can just use execute: with self.connection as cursor: cursor.execute(open(“schema.sql”, “r”).read()) though you may want to set psycopg2 to autocommit mode first so you can use the script’s own transaction management. It’d be nice if psycopg2 offered a smarter mode where it read the file in a statement-at-a-time and sent it to the DB, but … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)