psycopg2

Simplify database (psycopg2) usage by creating a module

假装没事ソ 提交于 2019-12-06 01:25:45
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.close() This will open a connection to the mydb database, select all fields from the table mytable and

ImportError: “No modules named”. But modules already installed in dist-packages

情到浓时终转凉″ 提交于 2019-12-06 00:48:01
问题 I am using python2.7 and trying to import modules such as psycopg2 . But I get the following error when I try to import the module: import psycopg2 ImportError: No module named psycopg2 When I try pip to install the module it gives me the following message: Requirement already satisfied (use --upgrade to upgrade): psycopg2 in /usr/local/lib/python2.7/dist-packages Cleaning up... Can anyone please tell me what I am doing wrong? 回答1: Is the module installed in your PYTHONPATH? You can verify

Error when importing CSV to postgres with python and psycopg2

放肆的年华 提交于 2019-12-05 22:11:20
I try to COPY a CSV file from a folder to a postgres table using python and psycopg2 and I get the following error: Traceback (most recent call last): File "<stdin>", line 1, in <module> psycopg2.ProgrammingError: must be superuser to COPY to or from a file HINT: Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone. I also tried to run it through the python environment as: constr = "dbname='db_name' user='user' host='localhost' password='pass'" conn = psycopg2.connect(constr) cur = conn.cursor() sqlstr = "COPY test_2 FROM '/tmp/tmpJopiUG/downloaded_xls.csv'

psycopg2 “TypeError: not all arguments converted during string formatting”

扶醉桌前 提交于 2019-12-05 20:08:04
问题 I'm trying to insert binary data (a whirlpool hash) into a PG table and am getting an error: TypeError: not all arguments converted during string formatting code: cur.execute(""" INSERT INTO sessions (identity_hash, posted_on) VALUES (%s, NOW()) """, identity_hash) I tried adding conn.Binary("identity_hash") to the variable before insertion, but get the same error. The identity_hash column is a bytea. Any ideas? 回答1: Have you taken a look at the "examples/binary.py" script in the psycopg2

python + psycopg2 = unknown types?

自作多情 提交于 2019-12-05 19:18:00
It seems when I use callproc(), psycopg2 isn't properly casting strings as text or character varying. For example: values = [pid, 4, 4, 'bureau ama', 0, 130, row['report_dte'], row['report_dte'], 1, 1, 1, None, None, 'published', row['report_dte']] cur.callproc('header', values) Yields: psycopg2.ProgrammingError: function header(integer, integer, integer, unknown, integer, integer, unknown, unknown, integer, integer, integer, unknown, unknown, unknown, unknown) does not exist LINE 1: SELECT * FROM header(509952,4... First, the "bureau ama" is unknown instead of text/character, and row["report

How to use server side cursors with psycopg2

坚强是说给别人听的谎言 提交于 2019-12-05 19:14:05
问题 I have a table with 4million rows and I use psycopg2 to execture a: SELECT * FROM ..WHERE query I haven't heard before of the server side cursor and I am reading its a good practice when you expect lots of results. I find the documentation a bit limited and I have some basic questions. First I declare the server-side cursor as: cur = conn.cursor('cursor-name') then I execute the query as: cur.itersize = 10000 sqlstr = "SELECT clmn1, clmn2 FROM public.table WHERE clmn1 LIKE 'At%'" cur.execute

Psycopg2 reporting pg_hba.conf error

放肆的年华 提交于 2019-12-05 18:15:38
问题 I've run into a weird situation while trying to use PostgreSQL and Psycopg2. For some reason, every time I attempt to connect to the postgre database via python, I get the following error: psycopg2.OperationalError: FATAL: no pg_hba.conf entry for host "127.0.0.1", user "steve", database "steve", SSL on FATAL: no pg_hba.conf entry for host "127.0.0.1", user "steve", database "steve", SSL off Naturally, I checked pg_hba.conf to see what the issue was, but everything appeared to be configured

Django exceeds maximum Postgres connections

梦想的初衷 提交于 2019-12-05 15:59:56
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': 5432, 'CONN_MAX_AGE': 60, } } This is how Gunicorn is started: gunicorn --bind 0.0.0.0:8080 --worker

Postgres/psycopg2 - Inserting array of strings

 ̄綄美尐妖づ 提交于 2019-12-05 14:43:20
问题 I'm using Postgres 9 and Python 2.7.2 along with psycopg2 and am trying to insert an array of string values with properly escaped quotation marks. Sample: metadata = {"Name": "Guest", "Details": "['One', 'Two', 'Three']"} cur.execute("insert into meta values ('%s');" % metadata) which throws the exception: psycopg2.ProgrammingError: syntax error at or near "One" LINE 1: "Details": "['One... ^ I've also tried using Postgres' E to escape along with backslashes, but haven't found the correct

Postgresql server remote connection

隐身守侯 提交于 2019-12-05 14:38:40
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. When I try to connect using psycopg2 on python: conn=psycopg2.connect('host=XX.XX.XX.XX port=5432