psycopg2

I have psycopg2 installed in virtual env, but still getting importerror psycopg2

点点圈 提交于 2019-12-25 06:47:32
问题 I followed the following steps: [I am using Windows 8, Python 2.7.6] I created a virtual environment venv and then did pip install on requirements.txt to install all the libraries. I had to explicitly create a file called config.py to set the os.environ variables. Then, I am doing import config in other files. I have installed postgresql and added bin folder to environment path psycopg2 was failing to install from running requirements.txt. So, I downloaded the zip with setup.py from online,

psycopg2 import error due to failure to load libraries

為{幸葍}努か 提交于 2019-12-25 03:47:33
问题 I have tried many ways of installing psycopg2 after having installed PostgreSQL using the one-click installer, but anyway I try confronts me with the same import error in python: ImportError: dlopen(/Library/Python/2.5/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: libpq.5.dylib Referenced from: /Library/Python/2.5/site-packages/psycopg2/_psycopg.so Reason: image not found I am on Mac OS X 10.5.8. I am using Python 2.5. I installed PostgreSQL from the installer (I did not port it

Python / SQLAlchemy format DateRange object

爷,独闯天下 提交于 2019-12-25 02:20:55
问题 I am using the DateRange column type to save reservations for a booking system. My DB is PosgreSQL and I use Python 3 with Flask and SQLAlchemy. Even though it should be best practice to use DateRange , I dont even find a documentation or anything helpful on how to work with these types. The SQLAlchemy documentation has only 2 sentences written that such a column type exists. A side note here: All resources, which I find only show that people usually use a start date and an end date. Is my

Insert into table with type casting and condition in INSERT statement

假如想象 提交于 2019-12-25 01:13:26
问题 Based on this previous question I am trying to compose an insert statement in Python using psycopg2 with some data. The query works beautifully in postgresql console but when I try it from psycopg2 I get an error. Here's the table i want to insert to create_test_table = """CREATE TABLE persons( name VARCHAR, age INT, dob DATE, sex VARCHAR);""" Here's the data I want to insert d_ = {"name":"Gino", "age": "", "dob": "na", "sex": "M"} And here's my query: cur.execute("""INSERT INTO persons (age

Psycopg ppygis select query

拥有回忆 提交于 2019-12-24 22:30:31
问题 I'm trying to setup a basic working postgis setup with python ppygis package. >>> import psycopg2 >>> import ppygis >>> connection = psycopg2.connect(database='spre', user='postgres') >>> cursor = connection.cursor() >>> cursor.execute('CREATE TABLE test (geometry GEOMETRY)') >>> cursor.execute('INSERT INTO test VALUES(%s)', (ppygis.Point(1.0, 2.0),)) >>> cursor.execute('SELECT * from test') >>> point = cursor.fetchone()[0] >>> print point 0101000000000000000000F03F0000000000000040 >>> I

read_sql query returns an empty dataframe after I pass parameters as a dict in python pandas

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 20:15:06
问题 I am trying to parameterize some parts of a SQL Query using the below dictionary: query_params = dict( {'target':'status', 'date_from':'201712', 'date_to':'201805', 'drform_target':'NPA' }) sql_data_sample = str("""select * from table_name where dt = %(date_to)s and %(target)s in (%(drform_target)s) ---------------------------------------------------- union all ---------------------------------------------------- (select *, from table_name where dt = %(date_from)s and %(target)s in ('ACT')

Concurrency issue with psycopg2, Redshift, and unittest

▼魔方 西西 提交于 2019-12-24 16:35:15
问题 I am in Python 2.7, using psycopg2 to connect to an Amazon Redshift database. I have unit tests, and in the setUp and tearDown methods for this test class, I drop the tables that were created for the purpose of this test. So the scheme is: def setUp(self): drop_specific_tables() create_specific_tables() def tearDown(self): drop_specific_tables() The reason for dropping in the setUp as well as tearDown is in case a test exits unsafely and skips tearDown we can still know that whenever it runs

Python psycopg2 insert NULL in some rows in postgresql table

帅比萌擦擦* 提交于 2019-12-24 11:54:03
问题 I have a Python dataframe with NULL value in some rows, while inserting to postgresql, some null in datetype column turns into 'NaT' string or 'NaN', I like it to be a real NULL , which is nothing in that cell. sample dataframe before insert import psycopg2 import pandas as pd import numpy as np conn=psycopg2.connect(dbname= 'myDB', host='amazonaws.com', port= '2222', user= 'mysuser', password= 'mypass') cur = conn.cursor() df= pd.DataFrame({ 'zipcode':[1,np.nan,22,88],'city':['A','h','B',np

Psycopg2: copy CSV data to table with extra column values

雨燕双飞 提交于 2019-12-24 09:03:56
问题 I am using the copy_expert method in psycopg2 to copy data from a CSV file to a postgresql table. I have postgres table like this: create table my_table ( cola text, colb text, colc text, cold text, cole text, colf text, colg text ) And a CSV containing data like for the first five columns like this: cola,colb,colc,cold,cole 1,foo,a,10,vvv 2,bar,b,20,www 3,baz,c,30,xxx 4,boo,d,40,yyy 5,baa,e,50,zzz I would like to copy the CSV data for the first five columns, while also specifying values for

Targeting specific values from JSON API and inserting into Postgresql, using Python

泪湿孤枕 提交于 2019-12-24 08:04:12
问题 Right now i am able to connect to the url api and my database. I am trying to insert data from the url to the postgresql database using psycopg2. I dont fully understand how to do this, and this is all i could come up with to do this. import urllib3 import json import certifi import psycopg2 from psycopg2.extras import Json http = urllib3.PoolManager( cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) url = '<API-URL>' headers = urllib3.util.make_headers(basic_auth='<user>:<passowrd>') r =