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

Getting a psycopg2 `connection` object from pytest-pgsql

Why I am trying to do this I want to abstract some operations on a database behind a python class called DataStore. This will have methods such as get_last_update_for_user(username: str) -> date that abstract away the need for anyone using the…
Harry Braviner
  • 627
  • 4
  • 12
1
vote
1 answer

cursor "_django_curs_1696_sync_2" does not exist

I accidentally deleted my migrations. I later makemigrations and migrate but when I access certain parts of the app that have ForeignKey references, I get cursor "_django_curs_1696_sync_2" does not exist. How on earth would I go about getting this…
fakeMake
  • 738
  • 8
  • 18
1
vote
1 answer

pandas to_sql with dict raises "can't adapt type 'dict'", is there a way to avoid `dumps`?

I am trying to load a simple pandas DataFrame on a PostgreSQL table (pandas 1.4, Postgres 13): df = pd.DataFrame([{"index": 1, "properties": {"a": 1, "b": 2}}]) df.to_sql("_test_table", con, if_exists="replace") However, I get ProgrammingError:…
astrojuanlu
  • 6,744
  • 8
  • 45
  • 105
1
vote
1 answer

SQL: How do I add different columns from different tables and make it one amount?

I am trying to add columns from different tables in order to get a total amount of all my streams. I've tried joining SQL JOIN them which works fine but it doesn't allow me to get the total number of streams. I've now decided to use union SQL UNION…
1
vote
2 answers

InvalidTextRepresentation: Invalid input syntax for type bigint:"All Forms"

I had a field in my model with book_classes = (("","Select Form"),("1",'F1'),("2",'F2'),("3",'F3'),("4",'F4'),("All Forms","All Forms")) b_classes = models.CharField('Form',max_length=9,choices=book_classes,default="n/a") And then changed it…
fakeMake
  • 738
  • 8
  • 18
1
vote
0 answers

Pandas UserWarning using psycopg2

I am new to using postgreSQL in Python and using Pandas. I am trying to create a pandas dataframe from a query, with the following: with conn.cursor() as cur: cur.execute('SELECT * FROM payment') postgres_df = pd.read_sql_query('SELECT * FROM…
David A.
  • 25
  • 4
1
vote
2 answers

ModuleNotFoundError: No module named 'psycopg2' by using Pyodide

I have only added this code to the *ts file of this repository(https://github.com/ibdafna/webdash) which works fine in my conda environment. from sqlalchemy.engine.create import create_engine import pandas as pd from datetime import datetime url =…
RenzoG
  • 31
  • 5
1
vote
1 answer

Psycopg2. Change cursor factory for a single query

I'm using RealDictRow cursor as default cursor. But For some specific queries regular (tuple) cursor is more handy. Is psycopg2 allows to replace cursor during a single query? Usage example: cursor.execute('SELECT id FROM posts',…
salius
  • 918
  • 1
  • 14
  • 30
1
vote
1 answer

Django can't connet to PostgreSQL Docker container via psycopg2

I am trying to migrate a django project on macOS Monterey 12.3, and I am having some troubles. It seems like psycopg2 doesn't want to connect to my docker container at all. Everytime it prints this error: django.db.utils.OperationalError: connection…
OctoBarn
  • 43
  • 1
  • 3
1
vote
0 answers

Why is xgboost working/not working depending on order of import with other packages?

When trying to import xgboost with the following: import pandas as pd, numpy as np, psycopg2, xgboost as xgb, os, shutil, pickle, random I get the following error: File…
1
vote
1 answer

How to rollback in psycopg2?

I´m forcing an error in qry2 so that a can run qry3 suposing it could rollback to save_1 but it doesn´t. Any catch? Using PostgreSql 14.2 qry1 = ('begin;' + 'savepoint save_1;' + 'delete from ntnb_cup;') qry2 = ... # Force…
Fcoatis
  • 85
  • 1
  • 2
  • 11
1
vote
1 answer

postgres plpgsql insert jsonb without escaping double quotes

I am using psycopg2 to interact with a postgres (v13) data base. I intend to dynamically create sql scripts to create tables, functions, etc. for execution in the db. I created as script to do an upsert to a table with two boolean and one jsonb…
MikeB2019x
  • 823
  • 8
  • 23
1
vote
0 answers

Django channels + Postgresql: Real-Time Database Messaging

I am currently developing an app with Django and PostgreSQL. Often, new information is sent to database (sometimes one time a day, sometimes over 30000 times a day). I have an UI (vue.js) and I want to see the data from the database showing to the…
artemonsh
  • 113
  • 3
  • 13
1
vote
1 answer

OpenTelemetry is not tracing SQL Statements while using cursor_factory as NamedTupleCursor

Kindly look at the code below. I'm using opentelemetry for tracing. Psycopg2Instrumentor for PostgreSQL tracing. Here only the "show server_version" SQL statement is getting traced. But the SQL statement in execute method is not traced. I think it's…
1
vote
1 answer

psycopg2 sql.Identifier fails executing when query is valid

I have a simple psycoph2 snippet which tries to grant usage on a schema but it displays syntax however the query is perfectly valid when executed on the terminal import psycopg2 from psycopg2 import Error, sql, extras,…
meWantToLearn
  • 1,691
  • 2
  • 25
  • 45