psycopg2

python-postgresql, seems to be connected but no respond

故事扮演 提交于 2019-12-11 15:50:01
问题 I am applying SQL commands with Python (PyCharm), but without getting any error, I get no respond from the database. Even if deliberately I use wrong password still no error, and in the run window I get "Process finished with exit code 0". There are two methods that I applied, the first initially worked properly. When I use pgAdmin4, I can apply the SQL commands without any problem. Two block of code are following, and they are for the same method. The first version worked once, but then I

Why does having no autocommit facility mean that all queries execute within a transaction in PostgreSQL?

霸气de小男生 提交于 2019-12-11 15:19:02
问题 From https://wiki.postgresql.org/wiki/Psycopg2_Tutorial PostgreSQL does not have an autocommit facility which means that all queries will execute within a transaction. Execution within a transaction is a very good thing, it ensures data integrity and allows for appropriate error handling. However there are queries that can not be run from within a transaction . Take the following example. #/usr/bin/python2.4 # # import psycopg2 # Try to connect try: conn=psycopg2.connect("dbname='template1'

Python, SQL: Set the columns read as parameter

吃可爱长大的小学妹 提交于 2019-12-11 13:27:08
问题 I have a SQL query, mad in Python using Psycopg2. The query reads some columns from the arches table: rows = archesDB.read_all("""SELECT "+str(columns)[1:-1].replace("'","")+" FROM arches WHERE lower(arch) like '%%%s%%'""" % (arch.lower())) I want to parametrize this query, so that it will not specify the columns needed using string concatenation, but as parameters - a far more elegant way. The naïve way is to SELECT * , and filter out the columns I need. But this burdens the DB and network

Can't install psycopg2 in virtualenv “error: stdarg.h: No such file or directory”

守給你的承諾、 提交于 2019-12-11 13:17:17
问题 NOTE: This same error also occurs when using easy_install, other ways of installation... I'm facing almost exactly the same problem as I'm trying to install psycopg2 onto Mac OS 10.6.3; it claims it can't find "stdarg.h" but I can see it's there; what should I do? , except I'm on OSX Lion (10.7) and I'm on Python v2.7. It's also seemingly related to Can't install psycopg2 with pip in virtualenv on Mac OS X 10.7 but I get a different error message. I made a new question because solutions that

String passed into cursor.callproc becomes unknown (psycopg2, python 2.7, postgres 9.3)

落花浮王杯 提交于 2019-12-11 12:24:35
问题 For some reason, passing a string from Python into a Postgres function and calling it using a psycopg2 cursor causes the function to be unrecognized because the argument is unknown. The function is very simple like so: CREATE OR REPLACE FUNCTION text_test (some_text text) RETURNS void AS $$ BEGIN END; $$ LANGUAGE plpgsql; On the python side, I have: cursor.callproc("text_test", ("test",)) And I get the following error: psycopg2.ProgrammingError: function text_test(unknown) does not exist LINE

How to insert a python list into Postgres table

孤者浪人 提交于 2019-12-11 10:18:24
问题 I am trying to determine the simplest way to insert data from a python list into a PostgreSQL table. My PostgreSQL table was created as follows; CREATE TABLE results(Id serial primary key, T0 timestamp, T1 real, T2 real, T3 real, T4 real, Total real, Result text); My python list is formatted as follows; ResultsList = ['2015-07-20 16:06:05', 0.16, 5.22, 5.81, 0.69, 11.90, 'OK'] I am using the following python code to write the list into the results table; import psycopg2 as dbapi con = dbapi

How to make psycopg2 emit nested quotes?

怎甘沉沦 提交于 2019-12-11 10:16:44
问题 As per "relation does not exist" in pg_table_size, I need to emit nested single and double quotes: import psycopg2 as pg from psycopg2 import sql conn = pg.connect("dbname=test user=test") table_name = "testDB" cu = conn.cursor() cu.execute(sql.SQL("SELECT pg_table_size(%s)"), (table_name,)) emits SELECT pg_table_size('testDB') which raises psycopg2.ProgrammingError: relation "testdb" does not exist while cu.execute(sql.SQL("SELECT pg_table_size({t})").format(t=sql.Identifier(table_name)))

How to use PostgreSQL in multi thread python program

半城伤御伤魂 提交于 2019-12-11 08:35:47
问题 I am using psycopg2 (2.6) connect to PostgreSQL database in a multi-threading python program. When queue size in program increase, select queries get error "no results to fetch", but inserts records to db works very well. example code: class Decoders(threading.Thread): def __init__(self, queue): threading.Thread.__init__(self) self.queue = queue def run(self): self.decode() def decode(self): queue = self.queue db = Database() while queue.qsize() > 0: # calling db methods, just an example temp

Postgres fails fetching data in Python

匆匆过客 提交于 2019-12-11 08:28:41
问题 I am using Python with psycopg2 module to get data from Postgres database. The database is quite large (tens of GB). Everything appears to be working, I am creating objects from the fetched data. However, after ~160000 of created objects I get the following error: I suppose the reason is the amount of data, but I could not get anywhere searching for a solution online. I am not aware of using any proxy and have never used any on this machine before, the database is on localhost . 回答1: It's

UPDATE json array using aggregrate function

送分小仙女□ 提交于 2019-12-11 07:03:51
问题 Working with Postgres 9.3, Python 2.7, psycopg2. I have a table called SomeTable with a json field some_json_array and row_id key. some_json_array looks something like this: "[{'key': 'value_one'}, {'key': 'value_two'}, etc]" I also have a function in which I'm trying to add some elements to the json array of SomeTable corresponding to the given row_id . My code is as follows: CREATE OR REPLACE FUNCTION add_elements (insertion_id smallint, new_elements_json json) RETURNS void AS $$ BEGIN