psycopg2

Simplify database (psycopg2) usage by creating a module

 ̄綄美尐妖づ 提交于 2019-12-07 15:02:40
问题 Let me preface this by saying that I am fairly new to Python and I apologize if this is not the appropriate place for this question. I am using the psycopg2 module to manipulate a PostgreSQL database. The general usage would look something like this: # Example 1 import psycopg2 conn = psycopg2.connect(database="mydb", user="postgres") cur = conn.cursor() cur.execute ("SELECT * FROM mytable;") rows = cur.fetchall() for i, row in enumerate(rows): print "Row", i, "value = ", row cur.close() conn

Postgresql server remote connection

孤街浪徒 提交于 2019-12-07 08:32:22
问题 I am trying to set up a postgresql 9.1 server on ubuntu, for remote access of data. I have postgres properly installed, the server process is running and I am trying to configure it so that I can access the server remotely over the internet from a few other computers outside my LAN. I have already modified my pg_hba.conf with: host all all 0.0.0.0 trust and the postgresql.conf with: listen_addresses = '*' port = 5432 I have additionally modified my iptables to accept connections on port 5432.

Inserting rows into db from a list of tuples using cursor.mogrify gives error

混江龙づ霸主 提交于 2019-12-07 08:22:28
I am trying to insert large number of rows into postgres using cursor.mogrify using this psycopg2: insert multiple rows with one query data is a list of tuples where each tuple is a row that needs to be inserted. cursor = conn.cursor() args_str = ','.join(cursor.mogrify("(%s,%s,%s,%s,%s,%s,%s,%s)", x) for x in data) cursor.execute( "insert into table1 (n, p, r, c, date, p1, a, id) values " + args_str)` but getting error : TypeError: sequence item 0: expected str instance, bytes found at line: args_str = ','.join(cursor.mogrify("(%s,%s,%s,%s,%s,%s,%s,%s)", x) for x in data) If I try to change

Python psycopg2 error vars function

扶醉桌前 提交于 2019-12-07 08:21:27
I have a piece of code that work fine on a server and don't work on other server ( Linux servers) import psycopg2,psycopg2.extras conn = psycopg2.connect("host=xx.x.x.x dbname=dev user=user password=pass" ) parentId='272' dbCur = conn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor) dbCur.execute('select * from "treeItem" where "parentId" = %s order by "order"',(parentId,)) for row in dbCur: print type(row) print row.__dict__ vars(row) dbCur.close() conn.close() The output on the server error is : class 'psycopg2.extras.Record' Traceback (most recent call last): File "test1.py", line 8,

psycopg2: can't adapt type 'numpy.int64'

我只是一个虾纸丫 提交于 2019-12-07 08:04:09
问题 I have a dataframe with the dtypes shown below and I want to insert the dataframe into a postgres DB but it fails due to error can't adapt type 'numpy.int64' id_code int64 sector object created_date float64 updated_date float64 How can I convert these types to native python types such as from int64 (which is essentially 'numpy.int64') to a classic int that would then be acceptable to postgres via the psycopg2 client. data['id_code'].astype(np.int) defaults to int64 It is nonetheless possible

Django exceeds maximum Postgres connections

自闭症网瘾萝莉.ら 提交于 2019-12-07 08:01:16
问题 I am experiencing a problem with a Django application that is exceeding the maximum number of simultaneous connections (100) to Postgres when running through Gunicorn with async eventlet workers. When the connection limit it reached the application starts returning 500 -errors until new connections can be established. This is my database configuration: DATABASES = { 'default': { 'ENGINE': 'django.contrib.gis.db.backends.postgis', 'NAME': 'django', 'USER': 'django', 'HOST': 'postgres', 'PORT':

psycopg2 cannot find any tables after connection

半世苍凉 提交于 2019-12-07 07:41:48
问题 I can connect to my database, but psycopg2 fails to find any of my tables. The following will error trying to get my users: import psycopg2 try: conn = psycopg2.connect("dbname='pdb' user='postgres' host='localhost' password='password'") except: print 'failed to connect' cur = conn.cursor() cur.execute(""" SELECT * from Users """) rows = cur.fetchall() for row in rows: print row[0] #Error: psycopg2.ProgrammingError: relation "users" does not exist LINE 1: SELECT * from Users # This also fails

Psycopg2 install with pip works but cannot import module on OS X 10.9

╄→гoц情女王★ 提交于 2019-12-07 02:22:03
问题 I've installed psycopg2 with pip install psycopg2 and it worked just fine. The install output had a couple of warning along the lines of In file included from ./psycopg/psycopg.h:33: ./psycopg/config.h:71:13: warning: unused function 'Dprintf' [-Wunused-function] static void Dprintf(const char *fmt, ...) {} ^ 1 warning generated. but in the end it says Successfully installed psycopg2 and it also appears when I run pip list . Now when I try to import it in python I get an error: $ python

psycopg2 import error when ubuntu upgraded to 17.10 (from 17.04)

五迷三道 提交于 2019-12-07 01:23:07
问题 Everything was working great until I upgraded the OS to Ubuntu 17.10. Now my Django project won't run (python manage.py runserver) because psycopg2 won't import. psycopg2 is already installed with pip (nothing has changed there). To be exact this is the error: lib/python3.5/site-packages/psycopg2/.libs/libresolv-2-c4c53def.5.so: symbol __res_maybe_init, version GLIBC_PRIVATE not defined in file libc.so.6 with link time reference 回答1: Try reinstalling psycopg2 . It looks like a dynamically

Is it possible to issue a “VACUUM ANALYZE <tablename>” from psycopg2 or sqlalchemy for PostgreSQL?

ⅰ亾dé卋堺 提交于 2019-12-07 00:53:55
问题 Well, the question pretty much summarises it. My db activity is very update intensive, and I want to programmatically issue a Vacuum Analyze. However I get an error that says that the query cannot be executed within a transaction. Is there some other way to do it? 回答1: This is a flaw in the Python DB-API: it starts a transaction for you. It shouldn't do that; whether and when to start a transaction should be up to the programmer. Low-level, core APIs like this shouldn't babysit the developer