PostgreSQL Database Wrapper
pgdb is a lightweight Python wrapper around `psycopg2-binary` designed to simplify basic interactions with PostgreSQL databases. It provides a simple API for connecting, executing queries, and fetching results. The library, currently at version 0.0.11, appears to be in maintenance mode with its last update over two years ago.
Common errors
-
ModuleNotFoundError: No module named 'pgdb'
cause The `pgdb` library is not installed in your Python environment.fixInstall the library using pip: `pip install pgdb` -
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: FATAL: password authentication failed for user "postgres"
cause Incorrect database connection parameters (host, port, database name, user, or password) or the PostgreSQL server is not running/accessible.fixDouble-check the `host`, `database`, `user`, and `password` arguments passed to `pgdb.connect()`. Ensure your PostgreSQL server is running and accessible from your application's environment. -
AttributeError: 'module' object has no attribute 'connect'
cause This error can occur if you attempt to access `connect` incorrectly, for example, after a `from pgdb import *` or if the module itself is shadowed.fixAlways import `pgdb` as a module (`import pgdb`) and then call the function via the module namespace (`pgdb.connect(...)`). Ensure no other file or module named `pgdb.py` is in your Python path that might shadow the installed library.
Warnings
- gotcha `pgdb` specifically requires `psycopg2-binary`. If you have `psycopg2` installed, `pgdb` might attempt to use it, but its `setup.py` explicitly lists `psycopg2-binary`.
- gotcha As a simple wrapper, `pgdb` offers basic database interaction but lacks advanced features like ORM capabilities, sophisticated connection pooling, or asynchronous operations found in larger, more actively maintained libraries (e.g., SQLAlchemy, asyncpg).
- deprecated The `pgdb` library has not been updated since February 2022, indicating a lack of active development or maintenance. This could lead to compatibility issues with newer Python versions or PostgreSQL features.
Install
-
pip install pgdb
Imports
- connect
import pgdb
Quickstart
import pgdb
import os
DB_HOST = os.environ.get('PGDB_HOST', 'localhost')
DB_NAME = os.environ.get('PGDB_DATABASE', 'test_db')
DB_USER = os.environ.get('PGDB_USER', 'postgres')
DB_PASS = os.environ.get('PGDB_PASSWORD', 'mysecretpassword') # REMEMBER to change in prod
conn = None
try:
conn = pgdb.connect(host=DB_HOST, database=DB_NAME, user=DB_USER, password=DB_PASS)
cursor = conn.cursor()
# Cleanup for re-runs and create table
cursor.execute("DROP TABLE IF EXISTS example_data;")
cursor.execute("CREATE TABLE example_data (id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL);")
conn.commit()
# Insert data
cursor.execute("INSERT INTO example_data (name) VALUES ('Alice'), ('Bob'), ('Charlie');")
conn.commit()
# Fetch and print data
cursor.execute("SELECT id, name FROM example_data;")
print("Fetched data:")
for row in cursor.fetchall():
print(row)
except pgdb.Error as e:
print(f"Database error: {e}")
if conn:
conn.rollback() # Rollback on error
finally:
if conn:
conn.close()