psycopg2

Setting application_name on Postgres/SQLAlchemy

爷,独闯天下 提交于 2019-12-30 02:33:06
问题 Looking at the output of select * from pg_stat_activity; , I see a column called application_name , described here. I see psql sets this value correctly (to psql ...), but my application code (psycopg2/SQLAlchemy) leaves it blank. I'd like to set this to something useful, like web.1 , web.2 , etc, so I could later on correlate what I see in pg_stat_activity with what I see in my application logs. I couldn't find how to set this field using SQLAlchemy (and if push comes to shove - even with

Execute .sql schema in psycopg2 in Python

自作多情 提交于 2019-12-30 01:40:14
问题 I have a PostgreSQL schema stored in .sql file. It looks something like: CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, facebook_id TEXT NOT NULL, name TEXT NOT NULL, access_token TEXT, created INTEGER NOT NULL ); How shall I run this schema after connecting to the database? My existing Python code works for SQLite databases: # Create database connection self.connection = sqlite3.connect("example.db") # Run database schema with self.connection as cursor: cursor.executescript(open(

Install pip on pypy

三世轮回 提交于 2019-12-29 18:54:08
问题 I want to speed up my program so i'm trying to setup pypy + psycopg2cffi. This program opens a xml, parses it and then insert some data in a database. I'm using currently python3, postgresql and psycopg2 but this approaches is really slow. So i want to try run my program with pypy + psycopg2cffi. I have python 3 and pypy, and i want to install psycopg2cffi so i ran this command: pip install psycopg2cffi psycopg2cffi-compat But psycopg2cffi was only installed on python because when i try to

psycopg2 leaking memory after large query

为君一笑 提交于 2019-12-29 18:42:07
问题 I'm running a large query in a python script against my postgres database using psycopg2 (I upgraded to version 2.5). After the query is finished, I close the cursor and connection, and even run gc, but the process still consumes a ton of memory (7.3gb to be exact). Am I missing a cleanup step? import psycopg2 conn = psycopg2.connect("dbname='dbname' user='user' host='host'") cursor = conn.cursor() cursor.execute("""large query""") rows = cursor.fetchall() del rows cursor.close() conn.close()

Illegal instruction: 4 when running Django

会有一股神秘感。 提交于 2019-12-29 09:22:18
问题 I made clean install of Django v1.11.10 now. When I run python manage.py runserver everything works fine. But when I try connect to Postgres database, I install package pip install psycopg2 , modify DATABASES varibale and after running runserver command it fails with Illegal instruction error: Performing system checks... System check identified no issues (0 silenced). Illegal instruction: 4 What is it? How to get log error? I use Mac OS 10.11.6, PostgresApp (tried on v9 and v10 server to

Installing psycopg2 (postgresql) in virtualenv on windows

喜你入骨 提交于 2019-12-29 05:05:29
问题 I installed psycopg2 in virtualenv using easy_install psycopg2 . I did not see any errors and looks like installation went fine.. there is an egg file created in the site-packages dir for psycopg2.. but when I run import psycopg2 in the interpreter, I am getting following error.. any clue? How can I fix it.. any other way to install psycopg2 in virtualenv.. Traceback (most recent call last): File "<stdin>", line 1, in <module> File "build\bdist.win32\egg\psycopg2\__init__.py", line 69, in

IntegrityError: distinguish between unique constraint and not null violations

独自空忆成欢 提交于 2019-12-28 06:44:09
问题 I have this code: try: principal = cls.objects.create( user_id=user.id, email=user.email, path='something' ) except IntegrityError: principal = cls.objects.get( user_id=user.id, email=user.email ) It tries to create a user with the given id and email, and if there already exists one - tries to get the existing record. I know this is a bad construction and it will be refactored anyway. But my question is this: How do i determine what kind of IntegrityError has happened: the one related to

Cannot install psycopg2 on OSX 10.6.7 with XCode4

天大地大妈咪最大 提交于 2019-12-28 05:34:05
问题 Trying to install psycopg2 on OSX results in the following: building 'psycopg2._psycopg' extension creating build/temp.macosx-10.6-universal-2.6 creating build/temp.macosx-10.6-universal-2.6/psycopg gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch ppc -arch x86_64 -pipe -DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.4 (dt dec pq3 ext)" -DPG_VERSION_HEX=0x090003 -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN

How can I use psycopg2.extras in sqlalchemy?

自闭症网瘾萝莉.ら 提交于 2019-12-27 12:27:12
问题 I want to upload a huge number of entries (~600k) into a simple table in a PostgreSQL DB, with one foreign key, a timestamp and 3 float per each entry. However, it takes 60 ms per each entry to execute the core bulk insert described here, thus the whole execution would take 10 h. I have found out, that it is a performance issue of executemany() method, however it has been solved with the execute_values() method in psycopg2 2.7. The code I run is the following: #build a huge list of dicts, one

How to retrieve passwords from a database

与世无争的帅哥 提交于 2019-12-25 07:48:18
问题 I am building a registration system for my web application wherein users provide a username and a password. This data is stored in a postgresql database. I am using bcrypt to generate a salted hash of the user entered password as follows import bcrypt hashed = bcrypt.hashpw(PasswordFromWebForm.encode('UTF-8'), bcrypt.gensalt()) This creates a salted password that looks something like this - b'$2b$12$GskbcRCMFHGuXumrNt3FLO' I am storing this value in a postgresql database. Next, when a user