{"id":15698036,"url":"https://github.com/prawn-cake/pgclient","last_synced_at":"2025-07-12T15:34:33.526Z","repository":{"id":57452116,"uuid":"41467144","full_name":"prawn-cake/pgclient","owner":"prawn-cake","description":"Yet another postgresql psycopg2 wrapper","archived":false,"fork":false,"pushed_at":"2016-02-28T19:47:31.000Z","size":47,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-15T06:48:10.645Z","etag":null,"topics":["postgres","postgresql","python"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prawn-cake.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-27T05:19:13.000Z","updated_at":"2018-04-18T21:32:42.000Z","dependencies_parsed_at":"2022-09-02T08:33:03.275Z","dependency_job_id":null,"html_url":"https://github.com/prawn-cake/pgclient","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/prawn-cake/pgclient","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prawn-cake%2Fpgclient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prawn-cake%2Fpgclient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prawn-cake%2Fpgclient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prawn-cake%2Fpgclient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prawn-cake","download_url":"https://codeload.github.com/prawn-cake/pgclient/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prawn-cake%2Fpgclient/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265013746,"owners_count":23698097,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["postgres","postgresql","python"],"created_at":"2024-10-03T19:22:53.080Z","updated_at":"2025-07-12T15:34:33.484Z","avatar_url":"https://github.com/prawn-cake.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"![pgclientIcon](https://www.dropbox.com/s/4l91lo7kt5xor4w/elephant_64.png?dl=1) pgclient\n========================================================================================\n[![Build Status](https://travis-ci.org/prawn-cake/pgclient.svg?branch=master)](https://travis-ci.org/prawn-cake/pgclient)\n[![Coverage Status](https://coveralls.io/repos/prawn-cake/pgclient/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/prawn-cake/pgclient?branch=master)\n![PythonVersions](https://www.dropbox.com/s/ck0nc28ttga2pw9/python-2.7_3.4-blue.svg?dl=1)\n\npgclient - yet another pool-based python2/3 compatible psycopg2 wrapper. \n\nThe client is fully based on thread-safe reliable connections pool and safe transactions executing\n\n*Tested on python2.7+, python3.4+*\n\n\nQuick start\n===========\n\n### System dependencies: ###\n\n* python-dev\n* libpq-dev\n \n\n### Install the package ###\n    \n    pip install pgclient\n\n\n### Initialize the client ###\n\n    from pgclient import PostgresClient\n    \n    \n    pg_client = PostgresClient(dsn='user=admin password=admin dbname=test host=localhost port=5432')\n    # OR\n    pg_client = PostgresClient(username='test', password='test', ...)\n    \n    with self.pg_client.get_cursor() as cursor:\n        cursor.execute('SELECT * FROM MyTable')\n        \n    result_set = cursor.fetchall()\n\nDatabase requests\n--------------------\n    \n**Assume that we use the following sql schema:**\n    \n    CREATE TABLE users (\n        id SERIAL, \n        username VARCHAR NOT NULL \n    )\n\n    \n**Cursor context manager**\n\n    with self.pg_client.get_cursor() as cursor:\n        cursor.execute('SELECT * FROM users')\n\n    users = cursor.fetchall()\n    username = users[0]['username']     \n    \n**NOTE:** Default *cursor_factory* is `psycopg2.extras.RealDictCursor`\n    \nTo override default factory, there are two ways:\n\n* Override default one for client instance\n\n        pg_client = PostgresClient(..., cursor_factory=psycopg2.extras.NamedTupleCursor)\n    \n* Override for context\n\n        with pg_client.get_cursor(cursor_factory=MyCursor) as cursor:\n            cursor.execute('SELECT * FROM users')\n        \n\nSafe transactions\n-----------------\n\nAll requests inside `with` context will be executed and automatically committed within one transaction \n(or rolled back in case if database errors)\n    \n    with self.pg_client.get_cursor() as transaction:\n        transaction.execute('INSERT INTO users VALUES name=\"Mark\"')\n        transaction.execute('INSERT INTO users VALUES name=\"Paolo\"')\n        transaction.execute('SELECT * FROM users')\n\n    users = transaction.fetchall()\n    \n    \nAuto-reconnect connection pool\n------------------------------\nStarting a new transaction, it guarantees that connection is alive\n\n    with self.pg_client.get_cursor() as cursor:\n        # connection is alive\n        cursor.execute(...)\n    \n    # Or manually\n    conn = self.pg_client.acquire_conn()\n    conn.execute(...)\n    ...\n    self.pg_client.release_conn(conn)\n    \n    \nExtended errors\n---------------\n\nInstead of basic `psycopg2.Error` based errors, [Extended exception classes](http://www.postgresql.org/docs/current/static/errcodes-appendix.html#ERRCODES-TABLE) have been added.\nSo now you will get more meaningful error information in case of any errors during \nthe postgres communication and use error handling in more flexible way.\n\nExample:\n    \n    from pgclient import exceptions as pg_exc\n    \n    try:\n        with self.pg_client.get_cursor() as transaction:\n            transaction.execute(...)\n    except pg_exc.IntegrityConstraintViolation as err:\n        logger.error(err.message, err.diag, err.pgcode)\n    except pg_exc.DataException as err:\n        ...\n\nTo catch all errors:\n    \n    try:\n        with self.pg_client.get_cursor() as transaction:\n            transaction.execute(...)\n    except pg_exc.PgClientError as err:\n        logger.error(err)\n        ... \n\nSystem test\n===========\nTo run integration test you need to install the following:\n \n* [Docker](https://www.docker.com/) \n* [Docker compose](https://docs.docker.com/compose/)\n\n\n**Run system test:**\n\n* Run postgresql container: `docker-compose up -d postgresql`\n* Run system tests: `make system_test`\n* Stop postgresql container: `docker-compose stop postgresql`\n\nTo test with *postgresql:9.0* run `postgresql_90` container with docker compose.\n\nBoth versions are being tested with travis ci.\n\n\n\nBug tracker\n===========\n\nWarm welcome to suggestions and concerns\n\nhttps://github.com/prawn-cake/pgclient/issues\n\n\nLicense\n=======\n\nMIT - http://opensource.org/licenses/MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprawn-cake%2Fpgclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprawn-cake%2Fpgclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprawn-cake%2Fpgclient/lists"}