psycopg2

How to persist data into a One-to-Many SELF referential with SQLAlchemy?

How to persist data into a One-to-Many SELF referential with SQLAlchemy? Question: I’m trying to persist a One-To-Many self-referential relationship. My table looks something like this: class Users(Base): __tablename__ = "users" id = Column(Integer, primary_key=True, unique=True) connected_ids = Column(Integer, ForeignKey("users.id")) connected_with = relationship("Users") I arrived at this format following this page in the docs for …

Total answers: 2

Using a buffer to write a psycopg3 copy result through pandas

Using a buffer to write a psycopg3 copy result through pandas Question: Using psycopg2, I could write large results as CSV using copy_expert and a BytesIO buffer like this with pandas: copy_sql = "COPY (SELECT * FROM big_table) TO STDOUT CSV" buffer = BytesIO() cursor.copy_expert(copy_sql, buffer, size=8192) buffer.seek(0) pd.read_csv(buffer, engine="c").to_excel(self.output_file) However, I can’t figure out …

Total answers: 2

Catch any of the errors in psycopg2 without listing them explicitly

Catch any of the errors in psycopg2 without listing them explicitly Question: I have a try and except block where I would like to catch only the errors in the psycopg2.errors and not any other error. The explicit way would be: try: # execute a query cur = connection.cursor() cur.execute(sql_query) except psycopg2.errors.SyntaxError, psycopg2.errors.GroupingError as err: …

Total answers: 2

Python's psycopg2 & logging: Logged at the DEBUG-level queries are recorded in bytes

Python's psycopg2 & logging: Logged at the DEBUG-level queries are recorded in bytes Question: I have connection to PostgreSQL database: LOGGER = Logger().LOGGER # … self.connection = psycopg2.connect(connection_factory=LoggingConnection, dbname=…, user=…, password=…, host=…, options="-c search_path=…") self.connection.initialize(LOGGER) self.cursor = self.connection.cursor() It’s Logger class: # some data _MODE = "w" _ENCODING = "utf-8" _FORMATTER = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)s] …

Total answers: 2

Extract query returning Decimal('value'), float is expected

Extract query returning Decimal('value'), float is expected Question: I saw a topic with the same question on Stackoverflow but I have a bit different question. My system locally return the value as float, but on GitHub Actions its Decimal. What could be the reason session = app.ReadOnlySession() query_ = text(query) result_proxy = session.execute(query_, fetched_options) res …

Total answers: 1

Using yield to run code after method of function execution

Using yield to run code after method of function execution Question: I’m trying to create a class method that can run some code after its execution. In pytest we have this functionality with fixtures: @pytest.fixture def db_connection(conn_str: str): connection = psycopg2.connect(conn_str) yield connection connection.close() # this code will be executed after the test is done …

Total answers: 2

Questions regarding transactions on psycopg2

Questions regarding transactions on psycopg2 Question: If I have a transaction that saves data in the database, but this transaction is part of another transaction that needs the id(fk) of that data to create a new data, but an error occurs at that moment, all transactions are rolled back or just the last transaction? Asked …

Total answers: 1

Syntax error while using psycopg2.copy_expert

Syntax error while using psycopg2.copy_expert Question: I use psycopg2 to call PostgreSQL COPY command from my programs written in Python, and so far I have used copy_from function in that package to achieve this. As I understand, in recent versions qualified names for tables, that is, scheman_name.table_name are no more authorized while using copy_from for …

Total answers: 1

Python Connection to Postgres over SSH- Incorrect port?

Python Connection to Postgres over SSH- Incorrect port? Question: I am trying to connect to a postgres database over an ssh server. On postico, it works, and looks something like this: When I copy the url from postico, it looks like this: postgresql+ssh://[email protected]/exampleusername:[email protected]/exampledb However, when I try to implement it in python, it connects successfully …

Total answers: 1

SQL psycopg2 insert variable that is a list of variable length into database

SQL psycopg2 insert variable that is a list of variable length into database Question: I am trying to write a row of observations into my database, but I have some unique variable called list_variable which is a list of strings that can be of length 1-3. So sometimes [‘string1’] but sometimes also [‘string1′,’string2’] or [‘string1′,’string2′,’string3’]. …

Total answers: 1