psycopg2

How to not show password in clear text when connecting to postgres via sqlalchemy, psycopg2?

浪尽此生 提交于 2019-12-13 07:15:40
问题 I'm currently connecting to a Postgres db from a Python script and I'm using sqlalchemy with psycopg2: postgresql+psycopg2://user:password@host:port/dbname[?key=value&key=value...] This Python script is available to users, and the password is shown in clear text. How can I use an encrypted password instead? 回答1: Generally, this is done in a few different ways. 1. Hide your database behind a REST API Basically, don't make the database directly accessible to users. Provide an interface like a

Parallel table update using python multiprocessing (PostGIS/PostgreSQL)

£可爱£侵袭症+ 提交于 2019-12-13 06:08:05
问题 Hi I have been using the multiprocessing approach for updating a huge postgis table from here and it works quite well, but my table doesn't seem to get updated unless I add a commit; statement at the end of procQuery = 'UPDATE city SET gid_fkey = gid FROM country WHERE ST_within((SELECT the_geom FROM city WHERE city_id = %s), country.the_geom) AND city_id = %s' % (self.a, self.a) like procQuery = 'UPDATE city SET gid_fkey = gid FROM country WHERE ST_within((SELECT the_geom FROM city WHERE

PostgresSQLfile permissions error using COPY

两盒软妹~` 提交于 2019-12-13 04:27:24
问题 I am using python to dump csv data into a database using Psycopg2. I need to give Postgres permission to a specific filepath in order to use the COPY command (documentation: https://www.postgresql.org/docs/10/static/sql-copy.html). I need to give permission to a specific directory path route and file to avoid the following error: COPY database.table_name FROM '/home/development_user/Documents/App/CSV/filename.csv' delimiter ',' csv header ERROR: could not open file "/home/development_user

question regarding postgresql sequences

独自空忆成欢 提交于 2019-12-13 04:22:14
问题 I have a question regarding postgresql sequences. For instance, for bigserial datatype, is it true that the sequence is advanced, then the number is retrieved and even if the insertion/committing is not successful, the sequence doesn't backtracks. Which means the next time I might be doing insertion to the table, that might be a gap in the sequence number. Theres a before insert row trigger on my table and Im using psycopg2. thanks in advance. 回答1: even if the insertion/committing is not

Psycopg2 returns none when execute many is combined with returning id

流过昼夜 提交于 2019-12-13 03:31:58
问题 I want to insert many values into a table using execute_values . I also need the ids of the affected rows. I want to use RETURNING id . I have set the fetch flag. Still, the result is None. postgres 11 python 3.6.9 psycopg2 '2.8.3 (dt dec pq3 ext lo64)' I create a table with CREATE TABLE dummy_table ( id serial, name text, age int ); The values and query are values = [('bob', 10), ('alice', 12)] query = 'INSERT INTO dummy_table (name, age) VALUES %s RETURNING id;' I call execute values like

COPY_FROM throws .read() call error on bulk data on psycopg2

寵の児 提交于 2019-12-13 03:09:20
问题 I am trying to port a mongodb over to a postgres. I am using COPY_FROM to insert bulk data faster, but I keep getting the same error: psycopg2.extensions.QueryCanceledError: COPY from stdin failed: error in .read() call My original code would try to populate the table in one go but if I split the data into batches, there's a threshold to which the error isn't thrown, which is around 3 million records per batch: # Base case (only 1 iteration) : throws .read() call error MAX_BATCH_SIZE = total

How to cast date to string in psycopg2?

落花浮王杯 提交于 2019-12-13 02:26:13
问题 I'm sure it has something to do with registering custom type cast as described here. However, I'm not sure how to do that. What I want to do is something like this: SELECT * FROM table and where a column is of date type, I want psycopg2 to convert it to Python string instead of datetime. 回答1: I found how to do it: def register_New_Date(): # Cast PostgreSQL Date as Python string # Reference: # 1. http://initd.org/psycopg/docs/extensions.html#psycopg2.extensions.new_type # 2. http://initd.org

Inserting strings into postgresql through pyscopg2

怎甘沉沦 提交于 2019-12-12 22:41:58
问题 I'm trying to insert some string values into a postgresql db. namely: macaddress, timestamp (date time format) type of device (string) subtype (string) example: INSERT INTO device (address, tstamp, type, subtype) VALUES ('00:00:00:00','2012-02-22 19:31:26','computer','notebook') I'm using python psycopg2 script to dump the data and the following error is appearing: c.executemany("INSERT INTO device VALUES (default, '%s','%s','%s','%s')", (mac, date, typedev, subtype,)) TypeError: not all

Flask before_request and teardown_request for DB connections

邮差的信 提交于 2019-12-12 18:40:20
问题 According to the Flaskr tutorial, db connection should be opened and closed before each session: @app.before_request def before_request(): g.db = connect_db() @app.teardown_request def teardown_request(exception): db = getattr(g, 'db', None) if db is not None: db.close() However, this is the case when using sqlite3. my question is: when using postressql and SqlAlchemy - do connections need to be opened and closed in the same way, or does SQLAlchemy takes care of connection management

psycopg2 to deal with timestamp without time zone

拥有回忆 提交于 2019-12-12 18:36:16
问题 I obtain a record from PostgreSQL, which its type is timestamp without time zone I am using psycopg2 I might get datetime object if I am using timestamp with time zone . But, currently, this is not the case. http://initd.org/psycopg/docs/usage.html#time-zones-handling I realize I am getting float type instead. How can I suppose to get value like from the given float? Jan 07, 2010 16:38:49 connection.cursor.execute(sql, data) records = connection.cursor.fetchall() # In PostgreSQL, type is