psycopg2

Connecting psycopg2 with Python in Heroku

大城市里の小女人 提交于 2019-12-12 18:18:53
问题 I've been trying for some days to connect my python 3 script to PostgresSQL database( psycopg2 ) in Heroku, without Django. I found some article and related questions, but I had to invest a lot of time to get something that I thought should be very straightforward, even for a newbie like me. I eventually made it work somehow but hopefully posting the question (and answer) will help other people to achieve it faster. Of course, if anybody has a better way, please share it. As I said, I had a

Psycopg2 copy_from throws DataError: invalid input syntax for integer

随声附和 提交于 2019-12-12 18:08:37
问题 I have a table with some integer columns. I am using psycopg2's copy_from conn = psycopg2.connect(database=the_database, user="postgres", password=PASSWORD, host="", port="") print "Putting data in the table: Opened database successfully" cur = conn.cursor() with open(the_file, 'r') as f: cur.copy_from(file=f, table = the_table, sep=the_delimiter) conn.commit() print "Successfully copied all data to the database!" conn.close() The error says that it expects the 8th column to be an integer and

Postgresql & psycopg2: database does not exist

别来无恙 提交于 2019-12-12 12:47:05
问题 I am trying to establish a connection to a database like this: psycopg2.connect(database="movies", user="lfcj", host="127.0.0.1"); my pg_hba.conf file has a line: TYPE __ DATABASE___USER__ADDRESS___METHOD local all lfcj peer I am trying to use peer identification, and my SO user name is also lfcj . When I log in postgresql like this, grant all privileges to lfcj , and run \list : psql lfcj -h 127.0.0.1 -d movies Enter password: 'myPassword' psql (9.4.1) SSL connection (protocol: TLSv1.2,

Return PostgreSQL UUID array as list with psycopg2

吃可爱长大的小学妹 提交于 2019-12-12 12:17:55
问题 I have an SQL statement which contains a subquery embedded in an ARRAY() like so: SELECT foo, ARRAY(SELECT x from y) AS bar ... The query works fine, however in the psycopg2 results cursor the array is returned as a string (as in "{1,2,3}" ), not a list. My question is, what would be the best way to convert strings like these into python lists? 回答1: It works for me without the need for parsing: import psycopg2 query = """ select array(select * from (values (1), (2)) s); """ conn = psycopg2

Django and PostgreSQL - value too long for type character varying(512)

偶尔善良 提交于 2019-12-12 10:48:31
问题 I am migrating from a test SQLite database to a PostgreSQL database. I have a sample object that is inserted in the database, that worked on SQLite but is giving me an error in PostgreSQL. Code snippet is: car = CarItem.objects.create( user = motor_trend, name = 'Camaro 2010', category = cars, condition = 'Used', price = '28,547.00', production_year = '2010', color_interior = 'Black', color_exterior = 'Inferno Orange Metallic', reference = 'PRC17288', location_of_creation = 'Undisclosed',

`TypeError: argument 2 must be a connection, cursor or None` in Psycopg2

被刻印的时光 ゝ 提交于 2019-12-12 10:39:13
问题 I have a heroku pipeline set up, and have just enabled review apps for it. It is using the same codebase as my staging and production apps, same settings files and everything. When the review app spins up, it can connect to the created DB and run migrations. When I try to connect to the app in the browser, I get `TypeError: argument 2 must be a connection, cursor or None` in `psycopg2/_json.py, register_json:139` Top of stack is: `django.contrib.sites.models._get_site_by_id`. I've attached

Postgres turn on log_statement programmatically

谁说胖子不能爱 提交于 2019-12-12 05:52:24
问题 I want to turn on logging of all SQL statements that modify the database. I could get that on my own machine by setting the log_statement flag in the configuration file, but it needs to be enabled on the user's machine. How do you enable it from program code? (I'm using Python with psycopg2 if it matters.) 回答1: The "it needs to be enabled on the user's machine" phrase is confusing, indeed... I assume you mean "from the user (client) side" . In Postgresql some server run-time parameters can be

How to install psycopg2 for python2.6 when there is also 2.7 installed?

柔情痞子 提交于 2019-12-12 05:15:44
问题 I tried to install psycopg2 for python2.6 but my system also has 2.7 installed. I did >sudo -E pip install psycopg2 Downloading/unpacking psycopg2 Downloading psycopg2-2.5.3.tar.gz (690kB): 690kB downloaded Running setup.py (path:/private/var/folders/yw/qn50zv_151bfq2vxhc60l8s5vkymm3/T/pip_build_root/psycopg2/setup.py) egg_info for package psycopg2 Installing collected packages: psycopg2 Running setup.py install for psycopg2 building 'psycopg2._psycopg' extension cc -DNDEBUG -g -fwrapv -Os

Install psycopg2 on Windows 8.1

喜夏-厌秋 提交于 2019-12-12 04:05:16
问题 I want to install psycopg2 on Windows 8.1 x64 computer and I'm using python 2.7.6 from python.org. As I launch the installer as administrator, I got this error, I press confirm and next button, but I can't choose or type my python directory. I have seen this post after I got those problem, Psycopg installation on windows, but it seems that that is a different case as mine. How to successfully install psycopg2 on Windows 8.1 x64 computer? 回答1: Install the binary distribution of psycopg2 for

Python function hangs when called from within sql function

给你一囗甜甜゛ 提交于 2019-12-12 03:17:01
问题 Colleauge of mine wrote for me python function to import data from files on server to postgresql db. His "primary" python function is writen in python script on disk, and is using psycopg2. This "primary" function is called from wrapper plpythonu function, defined with python 2 on the pg server. Basically it uses "COPY" to load data into my existing table. When I call the wrapper plpythonu function from my query window (pgAdmin), it finishes almost instantly (small test data), and loads the