Junior — Middle
Share your experience working with PostgreSQL database, including technologies used and scenarios of application.
sobes.tech AI
Answer from AI
In my projects with Python, I used PostgreSQL as the main relational database for structured data storage. I interacted with it using the psycopg2 library, which provides low-level access to the database and allows executing SQL queries directly.
I also used SQLAlchemy ORM for convenient object-relational mapping, which simplified data handling and improved code readability.
Use cases included:
- Storing user data and settings
- Logging events and analytics
- Managing transactions and complex queries with JOINs and aggregations
Example of connecting and executing a query with psycopg2:
import psycopg2
conn = psycopg2.connect(dbname="mydb", user="user", password="pass", host="localhost")
cur = conn.cursor()
cur.execute("SELECT id, name FROM users WHERE active = TRUE")
rows = cur.fetchall()
for row in rows:
print(row)
cur.close()
conn.close()