psycopg2

Can I use md5 authentication with psycopg2?

和自甴很熟 提交于 2020-01-01 09:33:28
问题 After two hours of reading documentation, source code and help-threads, I'm giving up. I can't get psycopg2 to authenticate with a md5-string. According to this thread I don't have to anything besides enabling md5-auth in pg_hba.conf . This is my current pg_hba.conf : # TYPE DATABASE USER CIDR-ADDRESS METHOD local all all md5 host all all 127.0.0.1/32 md5 host all all ::1/128 md5 host all all 0.0.0.0/0 md5 And I use psycopg2 like this: psycopg2.connect(host='localhost', port=5433, user='me',

Can I use md5 authentication with psycopg2?

 ̄綄美尐妖づ 提交于 2020-01-01 09:33:25
问题 After two hours of reading documentation, source code and help-threads, I'm giving up. I can't get psycopg2 to authenticate with a md5-string. According to this thread I don't have to anything besides enabling md5-auth in pg_hba.conf . This is my current pg_hba.conf : # TYPE DATABASE USER CIDR-ADDRESS METHOD local all all md5 host all all 127.0.0.1/32 md5 host all all ::1/128 md5 host all all 0.0.0.0/0 md5 And I use psycopg2 like this: psycopg2.connect(host='localhost', port=5433, user='me',

How do I get tables in postgres using psycopg2?

非 Y 不嫁゛ 提交于 2019-12-31 09:07:10
问题 Can someone please explain how I can get the tables in the current database? I am using postgresql-8.4 psycopg2. 回答1: This did the trick for me: cursor.execute("""SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'""") for table in cursor.fetchall(): print(table) 回答2: pg_class stores all the required information. executing the below query will return user defined tables as a tuple in a list conn = psycopg2.connect(conn_string) cursor = conn.cursor() cursor.execute(

psycopg2 copy_expert() - how to copy in a gzipped csv file?

天涯浪子 提交于 2019-12-31 03:41:12
问题 If my table is schema_one.table_five and my file name is file_to_import.csv.gz, what args do I give the copy_expert() cmd in order to copy the file contents into the table? Here's what I'm trying: this_copy = '''COPY schema_one.table_five FROM STDIN with CSV''' this_file = "file_to_import.csv.gz" con = psycopg2.connect(dbname=dbname, host=host, port=port, user=user, password=password) cur = con.cursor() cur.copy_expert(this_copy, this_file) This produces an error: cur.copy_expert(this_copy,

psycopg2 copy_expert() - how to copy in a gzipped csv file?

和自甴很熟 提交于 2019-12-31 03:41:09
问题 If my table is schema_one.table_five and my file name is file_to_import.csv.gz, what args do I give the copy_expert() cmd in order to copy the file contents into the table? Here's what I'm trying: this_copy = '''COPY schema_one.table_five FROM STDIN with CSV''' this_file = "file_to_import.csv.gz" con = psycopg2.connect(dbname=dbname, host=host, port=port, user=user, password=password) cur = con.cursor() cur.copy_expert(this_copy, this_file) This produces an error: cur.copy_expert(this_copy,

How to remove the quotes from a string for SQL query in Python?

我是研究僧i 提交于 2019-12-31 01:01:08
问题 I have a dictionary of database names. I take a name from the dictionary database_name = database_dict[i] lets say the value for database_name is 'foo' Using Psycopg2 I am executing a statement: cur.execute("INSERT INTO %s VALUES(...);", database_name) I get A syntax error at foo, because it should be "INSERT INTO foo VALUES" not "INSERT INTO 'foo' VALUES" Any advice how to pass in a string value for the name of the table and removing the single quotes? Should I place an escape character

Copy in Postgresql: Absolute Path Interpreted as Relative Path

喜欢而已 提交于 2019-12-30 11:21:30
问题 I am running this statement in a Django app: c = connections['default'].cursor() query="copy (select * from analysis.\"{0}\") to STDOUT DELIMITER ',' CSV HEADER;".format(view_name) with open(csvFile,'w') as f: c.copy_expert(query,f) f.close() It does not create the correct csv file. Some of the values appear to be in the wrong columns. I am trying to test the SQL statement by running it in POSTGRESQL: copy (select * from analysis."S03_2005_activity_140807_153431_with_geom") to 'C:

SQLAlchemy error: “argument formats can't be mixed” when inputting variables

我是研究僧i 提交于 2019-12-30 09:58:25
问题 I have a Python script that runs a pgSQL file through SQLAlchemy's connection.execute function. Here's the block of code in Python: results = pg_conn.execute(sql_cmd, beg_date = datetime.date(2015,4,1), end_date = datetime.date(2015,4,30)) And here's one of the areas where the variable gets inputted in my SQL: WHERE ( dv.date >= %(beg_date)s AND dv.date <= %(end_date)s) When I run this, I get a cryptic python error: sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) argument formats

how to store binary file recieved by Flask into postgres

不羁的心 提交于 2019-12-30 07:17:24
问题 I currently have a Flask route that reveives file content via POST, and that stores it on the file system, ex: @app.route('/upload', methods=['POST']) def upload_file(): def allowed_file(f): return True file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(upload_dir(), filename)) return "", 200 I would like to store it in a BYTEA column in postgres, I am not sure how to bind the "data" argument to the insert

how to store binary file recieved by Flask into postgres

风格不统一 提交于 2019-12-30 07:17:08
问题 I currently have a Flask route that reveives file content via POST, and that stores it on the file system, ex: @app.route('/upload', methods=['POST']) def upload_file(): def allowed_file(f): return True file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(upload_dir(), filename)) return "", 200 I would like to store it in a BYTEA column in postgres, I am not sure how to bind the "data" argument to the insert