
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
pgsql is a simple PostgreSQL client library for Python 3. Written in pure Python and not having any dependencies, it should run on just about any implementation of the language.
Limitations:
$ pip install pgsql
import pgsql
# connect using a Unix domain socket
db = pgsql.Connection("/var/run/postgresql/.s.PGSQL.5432")
# execute multiple statements in a single call
db.execute("""
CREATE TABLE people (name TEXT UNIQUE, year INT);
INSERT INTO people VALUES ('Veronica', 1986);
""")
# execute a statement and iterate through the resulting rows
for person in db("SELECT * FROM people"):
print(person)
# close the connection
db.close()
# connect using a TCP socket, TLS and password authentication
# connections support the with statement
with pgsql.Connection(("localhost", 5432), "user", "password", tls = True) as db:
# ... as do transactions and prepared statements
with db.transaction():
with db.prepare("INSERT INTO people VALUES ($1, $2)") as insert_person:
for person in ("Wallace", 1986), ("Keith", 1959), ("Lianne", 1960):
insert_person(*person)
# transactions can also be done using .begin(), .commit() and .rollback() manually
db.begin()
db("INSERT INTO people VALUES ($1, $2)", "Backup", 1999)
db.rollback()
# connect using a TCP socket, TLS and certificate authentication
with pgsql.Connection(("localhost", 5432), tls_cert = "postgres.pem") as db:
# prepare a statement
people = db.prepare("SELECT * FROM people")
# execute the statement and iterate through the resulting rows
for person in people():
# rows are dataclass instances, columns their attributes
print(person.name, person.year)
people.close()
# return all rows
with db.prepare("SELECT * FROM people WHERE year > $1") as people_born_after:
print(list(people_born_after(1980))) # mutable
print(tuple(people_born_after(1980))) # immutable
# return a single row
with db.prepare("SELECT * FROM people WHERE name = $1") as person:
print(person("Veronica").row())
# return a single column
with db.prepare("SELECT year FROM people WHERE name = $1") as year:
print(year("Veronica").col())
# show the execution plan of a statement
print(db.explain("SELECT * FROM people"))
Row(name='Veronica', year=1986)
Veronica 1986
Wallace 1986
Keith 1959
Lianne 1960
[Row(name='Veronica', year=1986), Row(name='Wallace', year=1986)]
(Row(name='Veronica', year=1986), Row(name='Wallace', year=1986))
Row(name='Veronica', year=1986)
1986
[
{
"Plan": {
"Node Type": "Seq Scan",
"Parallel Aware": false,
"Async Capable": false,
"Relation Name": "people",
"Alias": "people",
"Startup Cost": 0.00,
"Total Cost": 22.70,
"Plan Rows": 1270,
"Plan Width": 36
}
}
]
Rows are returned as dataclass instances. When preparing a statement, it's possible to set a base for the dataclass generated for that particular statement. Defining fields for the base is not necessary, but if defined, their order must match that of the columns returned by the statement.
from dataclasses import dataclass, field
# create a base dataclass
@dataclass
class Person:
age: int = field(init = False)
def __post_init__(self):
self.age = 2004 - self.year
# return rows as Persons
with db.prepare("SELECT name, year FROM people", Person) as people:
for person in people():
print(person)
Person(age=18, name='Veronica', year=1986)
Person(age=18, name='Wallace', year=1986)
Person(age=45, name='Keith', year=1959)
Person(age=44, name='Lianne', year=1960)
PostgreSQL | Python |
---|---|
integers, serials, oid | int |
numeric, decimal | float |
boolean | bool |
Type binding is defined in the pgsql.types
dictionary, where keys
refer to pg_type row identifiers and values are callables used for type
conversion. By default, all non-numeric, non-boolean types are
returned as strings. Extending the dictionary is simple – for example, to
automatically decode returned json data:
import pgsql
import json
# SELECT oid FROM pg_type WHERE typname = 'json' returns 114
pgsql.types[114] = json.loads
Type binding only applies to retrieved data. Sent data (statement arguments) must have a string representation conforming to the lexical structure of constants in PostgreSQL.
Caught protocol errors and server error messages raise an exception indicating the severity of the error:
pgsql.Error
caused the current command to abortpgsql.Fatal
caused the current session to abortpgsql.Panic
caused all sessions to abortServer error messages include an error code. Server notice messages are ignored.
pgsql.Fatal
and pgsql.Panic
exception classespgsql.Statement
pgsql.types
to cover more numeric typespgsql.Connection
FAQs
PostgreSQL client library for Python 3
We found that pgsql demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.