Skip to main content

PostgreSQL client library for Python 3

Project description

PostgreSQL client library for Python 3. Simple, fast and lightweight.


$ pip install pgsql


import pgsql

# address defaults to ("localhost", 5432), a string must point to a unix socket
# user defaults to "postgres"
# password defaults to None
# database equals user by default
db = pgsql.Connection(user = "antti", database = "postgres")
print(db("CREATE TABLE people (name TEXT, age INT)"))
print(db("INSERT INTO people (name, age) VALUES ($1, $2)", "Veronica", 18))
print(db("SELECT * FROM people"))

# for convenience, connection objects support the with statement
with pgsql.Connection(user = "antti", database = "postgres") as db:
        # you can use .begin(), .commit(), .rollback() manually, or use the with statement
        with db.transaction():
                with db.prepare("INSERT INTO people (name, age) VALUES ($1, $2)") as ps:
                        for person in ("Wallace", 18), ("Keith", 45), ("Lianne", 44):

        # iterate through and print all the rows represented as tuples
        people = db.prepare("SELECT * FROM people")
        for person in people():

        # sometimes instead of an iterator, you want the rows as a list
        # you may also want to call columns by their name
        people_over = db.prepare("SELECT * FROM people WHERE age > $1").all
        for person in people_over(21):
                print(, "is", person.age - 21, "years over the age of 21")

        # when the expected result is only one row, it's convenient to call .first
        person_named = db.prepare("SELECT * FROM people WHERE name = $1 LIMIT 1").first


[('Veronica', 18)]
('Veronica', 18)
('Wallace', 18)
('Keith', 45)
('Lianne', 44)
Keith is 24 years over the age of 21
Lianne is 23 years over the age of 21
('Veronica', 18)


1.2 (2019-10-27)

  • Update package description

1.1 (2014-03-26)

  • Make it possible to execute one-time statements by calling the Connection object

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

pgsql-1.2.tar.gz (5.2 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page