postgres is a high-value abstraction over the psycopg2 database driver, simplifying interactions with PostgreSQL databases. Currently at version 4.0, the library offers a more Pythonic API, an Object-Relational Mapper (ORM), and improved cursor management. Releases are made as needed, focusing on usability and PostgreSQL-specific features.
pip install postgresVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to connect to a PostgreSQL database using `postgres.py`, create a table, insert data, and fetch results using `run()`, `one()`, and `all()` methods. It also illustrates the use of bind parameters for safe query execution.
Review the official documentation for `one` method usage in 2.0.0+ and update your calls accordingly.
Rename any existing `max_age` parameters in your `one()` or `all()` calls to avoid conflicts with the new library argument.
Replace all calls to `db.rows()` with `db.all()`.
Ensure `psycopg2-binary` is installed (`pip install psycopg2-binary`). If you need to build `psycopg2` from source for production, consider explicit installation or managing dependencies carefully to avoid conflicts with `psycopg2-binary`.
Update your code to always expect a list as the return value from `db.check_registration()`.
Always use bind parameters (e.g., `db.run("INSERT INTO users VALUES (%(name)s)", name=user_input)`) instead of f-strings or string formatting to embed values directly into SQL queries.Run `pip install postgres` to install the library.
Replace calls to `db.rows()` with `db.all()`.
Verify that your PostgreSQL server is running, listening on the expected host/port (e.g., `localhost:5432`), and check firewall rules. Also, ensure the connection string is correct.
Create the database if it doesn't exist, or correct the database name in your connection string. Ensure the connecting user has appropriate permissions.
Use parameterized queries with `%(param_name)s` placeholders and pass parameters as a dictionary to prevent misinterpretation and SQL injection. Example: `db.run("INSERT INTO table (col) VALUES (%(value)s)", value='my_string')`.