Skip to main content

Python Postgres aims to provide the simplest way to interact with PostgreSQL databases.

Project description

Python

Python Postgres


Python Postgres is an abstraction over psycopg and aims to provide the simplest way to interact with PostgreSQL databases in Python.

I have just started this project, and it is not ready for production use. I am still working on every aspect of it and may abandon it at any point without warning.


Installation

pip install python_postgres 

Basic Usage

from python_postgres import Postgres

pg = Postgres("pgadmin", "password", "pg-is-king.postgres.database.azure.com")


async def main():
    await pg.open()  # Open the connection pool, this requires a running event loop.
    files = await pg("SELECT * FROM files")
    await pg("INSERT INTO files (name, size) VALUES (%s, %s)", [("file1", 1024), ("file2", 2048)])

    async with pg.transaction() as tran:
        file_id = (
            await tran(
                "INSERT INTO files (name, size) VALUES VALUES (%s, %s) RETURNING file_id;",
                ("you_may_not_exist", 0),
            )
        )[0]
        await tran("INSERT INTO pages (page_number, file_id) VALUES (%s, %s);", (4, file_id))
        raise ValueError("Oopsie")
    await pg.close()  # Close the connection pool. Python Postgres will attempt to automatically
    # close the pool when the instance is garbage collected, but this is not guaranteed to succeed.
    # Be civilized and close it yourself.

A more in-depth look

The basic idea of this project is to provide one callable instance of the Postgres class. The Postgres class manages a connection pool in the background and will get a connection from the pool when called, spawn a binary cursor on it, run your query, return the results (or the number of rows affected), and then return the connection to the pool. As a query, you can pass either a literal - string or bytes - or a SQL or Composed object from the psycopg library.

In Essence, the Postgres class is syntactic sugar for turning this:

async def exec_query(
        query: LiteralString | bytes | SQL | Composed,
        params: tuple | list[tuple],
        is_retry: bool = False,
) -> list[tuple]:
    try:
        async with con_pool.connection() as con:  # type: psycopg.AsyncConnection
            async with con.cursor(binary=True) as cur:  # type: psycopg.AsyncCursor
                if isinstance(params, list):
                    await cur.executemany(query, params)
                else:
                    await cur.execute(query, params)
                await con.commit()
                return (
                    await cur.fetchall()
                    if cur.pgresult and cur.pgresult.ntuples > 0
                    else cur.rowcount or -1
                )
    except psycopg.OperationalError as error:
        if is_retry:
            raise IOError from error
        await con_pool.check()
        await exec_query(query, params, True)
    except psycopg.Error as error:
        raise IOError from error


await exec_query("SELECT * FROM files WHERE id = %s", (1234,))

into

await pg("SELECT * FROM files WHERE id = %s", (1234,))

Notes

Other than providing simpler syntax through a thin abstraction, this project inherits all the design choices of psycopg, including the caching of query execution plans

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_postgres-0.0.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

python_postgres-0.0.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file python_postgres-0.0.1.tar.gz.

File metadata

  • Download URL: python_postgres-0.0.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.6 Windows/10

File hashes

Hashes for python_postgres-0.0.1.tar.gz
Algorithm Hash digest
SHA256 1f6bbd209995005b41649f395d5ef77e517878774ea4ed6f2cabe53c6219ad2f
MD5 5006f19ff6bccf7cb5760358c746376c
BLAKE2b-256 38a9fc51990c747d71fa5cde9473eff991d6f631eae1f22ca58ff628e15b707e

See more details on using hashes here.

File details

Details for the file python_postgres-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_postgres-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e6aa8d6604914689044b44d75716eb3a51cd7f8156b7b93f1cca70d3e14093ad
MD5 984b4981a37b91422146dad2be047279
BLAKE2b-256 3feec02284110a76b19f0d8719b870aee4859a1f801a0416973dfe3b38720011

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page