Questions tagged [psycopg2]

Psycopg is a PostgreSQL adapter for Python programming language. It implements PEP 249 with many extensions.

Psycopg is a PostgreSQL adapter for Python . It is a wrapper around the libpq library. Psycopg fully implements PEP 249. It is thread-safe. It supports asynchronous I/O, server-side cursors, COPY, large objects and two-phase commit. Psycopg is extensible with new adapters and typecasters.

Questions that relate directly to psycopg2 should receive this tag. If you are using a web framework, scientific library or ORM in conjunction with Psycopg2 be sure to include that information in your post, and consider adding tags for those libraries.

Psycopg can be easily installed on Linux or Mac, but lacks full support in Windows, though there is a port available. It requires some manual installation in a virtual environment.

Psycopg2 can be used to archive the state of objects, although it is often convenient to use an ORM (SQLAlchemy for example).

External Links

4351 questions
1
vote
0 answers

Sqlalchemy: Numeric Value out of range

When i try to add user with BIG INT, psycopg2 throws Numberic Value out of range error. but still BIGINT is my data type in Column of my table and in local postgresql database. Edit1: I have added the source code for chat_members model. My table…
1
vote
1 answer

Why are the `keepalives` params in `psycopg2.connect(...)` required to run long running postgres queries in docker (ubuntu:18.04)?

We just transitioned to using Docker for development and are using the ubuntu:18.04 image. We noticed that queries using psycopg2 failed after a few minutes. This answer solved the problem using the following keepalives params: self.db =…
André C. Andersen
  • 8,955
  • 3
  • 53
  • 79
1
vote
1 answer

Postgres server-side cursor with LEFT JOIN does not return on Heroku PG

I have a Heroku app that uses a psycopg server-side cursor together with a LEFT JOIN query running on Heroku PG 13.5. The query basically says “fetch items from one table, that don’t appear in another table”. My data volume is pretty stable, and…
Derek Hill
  • 5,965
  • 5
  • 55
  • 74
1
vote
0 answers

Problem with installing psycopg2cffi on m1

Does anyone try to install psycopg2cffi on mac m1? psycopg2 works fine for me, for psycopg2cffi I got this error I'm doing pip3 install psycopg2cffi and then get ld: warning: dylib (/opt/homebrew/lib/libpq.dylib) was built for newer macOS…
Jakub Rybiński
  • 361
  • 2
  • 9
1
vote
0 answers

how to fix a operational_error?

this is my code to search wallstreetbets reddit page from psaw import PushshiftAPI import config import psycopg2 import psycopg2.extras connection = psycopg2.connect(host=config.DB_HOST, database=config.DB_NAME, user=config.DB_USER,…
1
vote
2 answers

postgres / psycopg: Date format error importing CSV

I'm using psycopg (v3) in python 3.10, working in Pycharm. I made a table and am trying to import a .csv; I'm getting this error: invalid input syntax for type date: "01/31/22" CONTEXT: COPY test_table, line 1, column quote_date: "01/31/22" First…
dbkc
  • 13
  • 3
1
vote
1 answer

Having problems with saving a list of strings to postgresql database

I've been struggling with this problem for like an hour, and I still don't know how to resolve it. The problem is that I have to store into a database a list of strings, but I don't know how to do it without triggering an error because it says…
mss051
  • 93
  • 9
1
vote
0 answers

psycopg2.OperationalError: connection to server failed: Operation timed out Is the server running on that host and accepting TCP/IP connections?

I have created an DB instance as follows: with the security groups below. With the inbound rule set to my IP. However when using : import psycopg2 import logging engine = psycopg2.connect( database="postgres", user="xxxxx", …
1
vote
0 answers

How to solve can't adapt type 'Series' in psycopg2

I want to insert a record with psycopg2. The code is simple: import psycopg2 query = """ INSERT INTO xxx (lon,lat) VALUES(%s,%s)""" cursor.execute(query, (df2['lon'],df2['lat'])) conn.commit() cursor.close() conn.close() However the code…
bibiji
  • 147
  • 1
  • 9
1
vote
1 answer

TypeError: not all arguments converted during string formatting - psycopg2 - mogrify - python 3.9.5

I want to save a list of strings to a postgresDB with psycopg2 in python 3.9.5. When I store the data as a python list and send it to the database with execute I get the following error. TypeError: not all arguments converted during string…
Brian Hode
  • 75
  • 1
  • 5
1
vote
2 answers

Passing JSON file in Postgres with Python

connection = psycopg2.connect("dbname=db1 user=postgres password=postgres") cursor = connection.cursor() cursor.execute("set search_path to public") with open('json_template') as file: data = file.read() query_sql = """ insert into table1…
1
vote
1 answer

Getting connection timeout error while connecting Azure Postgres DB in Python

I am trying to connect Azure Postgres DB from Python using psycopg2 library but getting below error. Could someone help on this. Error: psycopg2.OperationalError: connection to server at "xxx.postgres.database.azure.com" (xx.xx.xxx.xxx), port 5432…
Rahul Jha
  • 874
  • 1
  • 11
  • 28
1
vote
1 answer

Does mogrify fix the concern with injection attacks?

I understand that psycopg2 queries should not be formed by text replacement like f-strings, %s forms, etc. for fear of injection attacks. The docs make that clear. However, what's not clear to me is if the cursor.mogrify method is subject to the…
Lorem Ipsum
  • 4,020
  • 4
  • 41
  • 67
1
vote
0 answers

How does fetching with Psycopg and rendering with Jinja work?

Recently I was building a simple web blog using Flask and PostgreSQL and I stumbled upon something which seems to me like a weird behavior. @app.route('/') def index(): conn = get_connection() cur = conn.cursor() cur.execute("SELECT *…
samwell
  • 11
  • 1
1
vote
1 answer

How to UPDATE table in psycopg2

def GetUpdate(): enter = input("What attributes do you want to change ? ") select,p_id = input("Please enter your attributes separated by space: ").split() try: cur.execute("UPDATE Diary SET %s = %s where diary_id =…
Dias
  • 21
  • 5