Package: pg8000
pg8000 is a pure-PythonPostgreSQLdriver that complies withDB-API 2.0. It is tested on Python
versions 3.8+, on CPython and PyPy, and PostgreSQL versions 12+. pg8000’s name comes
from the belief that it is probably about the 8000th PostgreSQL interface for Python.
pg8000 is distributed under the BSD 3-clause license.All bug reports, feature requests and contributions are welcome athttp://github.com/tlocke/pg8000/.Table of ContentsInstallationNative API Interactive ExamplesBasic ExampleTransactionsQuery Using FunctionsInterval TypePoint TypeClient EncodingJSONRetrieve Column Metadata From ResultsNotices And NotificationsParameter StatusesLIMIT ALLIN and NOT INMany SQL Statements Can’t Be ParameterizedCOPY FROM And TO A StreamExecute Multiple SQL StatementsQuoted Identifiers in SQLCustom adapter from a Python type to a PostgreSQL typeCustom adapter from a PostgreSQL type to a Python typeCould Not Determine Data Type Of ParameterPrepared StatementsUse Environment Variables As Connection DefaultsConnect To PostgreSQL Over SSLServer-Side CursorsBLOBs (Binary Large Objects)Replication ProtocolDB-API 2 Interactive ExamplesBasic ExampleQuery Using FunctionsInterval TypePoint TypeNumeric Parameter StyleAutocommitClient EncodingJSONRetrieve Column Names From ResultsCOPY from and to a fileServer-Side CursorsBLOBs (Binary Large Objects)Type MappingTheory Of OperationNative API DocsDB-API 2 DocsDesign DecisionsTestsREADME.rstDoing A Release Of pg8000Release NotesInstallationTo install pg8000 usingpiptype:pip install pg8000Native API Interactive Examplespg8000 comes with two APIs, the native pg8000 API and the DB-API 2.0 standard
API. These are the examples for the native API, and the DB-API 2.0 examples
follow in the next section.Basic ExampleImport pg8000, connect to the database, create a table, add some rows and then
query the table:>>> import pg8000.native
>>>
>>> # Connect to the database with user name postgres
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> # Create a temporary table
>>>
>>> con.run("CREATE TEMPORARY TABLE book (id SERIAL, title TEXT)")
>>>
>>> # Populate the table
>>>
>>> for title in ("Ender's Game", "The Magus"):
...     con.run("INSERT INTO book (title) VALUES (:title)", title=title)
>>>
>>> # Print all the rows in the table
>>>
>>> for row in con.run("SELECT * FROM book"):
...     print(row)
[1, "Ender's Game"]
[2, 'The Magus']
>>>
>>> con.close()TransactionsHere’s how to run groups of SQL statements in atransaction:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("START TRANSACTION")
>>>
>>> # Create a temporary table
>>> con.run("CREATE TEMPORARY TABLE book (id SERIAL, title TEXT)")
>>>
>>> for title in ("Ender's Game", "The Magus", "Phineas Finn"):
...     con.run("INSERT INTO book (title) VALUES (:title)", title=title)
>>> con.run("COMMIT")
>>> for row in con.run("SELECT * FROM book"):
...     print(row)
[1, "Ender's Game"]
[2, 'The Magus']
[3, 'Phineas Finn']
>>>
>>> con.close()rolling back a transaction:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> # Create a temporary table
>>> con.run("CREATE TEMPORARY TABLE book (id SERIAL, title TEXT)")
>>>
>>> for title in ("Ender's Game", "The Magus", "Phineas Finn"):
...     con.run("INSERT INTO book (title) VALUES (:title)", title=title)
>>>
>>> con.run("START TRANSACTION")
>>> con.run("DELETE FROM book WHERE title = :title", title="Phineas Finn")
>>> con.run("ROLLBACK")
>>> for row in con.run("SELECT * FROM book"):
...     print(row)
[1, "Ender's Game"]
[2, 'The Magus']
[3, 'Phineas Finn']
>>>
>>> con.close()NB. There isa longstanding bugin the PostgreSQL server whereby if aCOMMITis issued against a failed
transaction, the transaction is silently rolled back, rather than an error being
returned. pg8000 attempts to detect when this has happened and raise anInterfaceError.Query Using FunctionsAnother query, using some PostgreSQL functions:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("SELECT TO_CHAR(TIMESTAMP '2021-10-10', 'YYYY BC')")
[['2021 AD']]
>>>
>>> con.close()Interval TypeA query that returns the PostgreSQL interval type:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> import datetime
>>>
>>> ts = datetime.date(1980, 4, 27)
>>> con.run("SELECT timestamp '2013-12-01 16:06' - :ts", ts=ts)
[[datetime.timedelta(days=12271, seconds=57960)]]
>>>
>>> con.close()Point TypeA round-trip with aPostgreSQL pointtype:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("SELECT CAST(:pt as point)", pt=(2.3,1))
[[(2.3, 1.0)]]
>>>
>>> con.close()Client EncodingWhen communicating with the server, pg8000 uses the character set that the server asks
it to use (the client encoding). By default the client encoding is the database’s
character set (chosen when the database is created), but the client encoding can be
changed in a number of ways (eg. settingCLIENT_ENCODINGinpostgresql.conf).
Another way of changing the client encoding is by using an SQL command. For example:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("SET CLIENT_ENCODING TO 'UTF8'")
>>> con.run("SHOW CLIENT_ENCODING")
[['UTF8']]
>>>
>>> con.close()JSONJSONalways comes back
from the server de-serialized. If the JSON you want to send is adictthen you can
just do:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> val = {'name': 'Apollo 11 Cave', 'zebra': True, 'age': 26.003}
>>> con.run("SELECT CAST(:apollo as jsonb)", apollo=val)
[[{'age': 26.003, 'name': 'Apollo 11 Cave', 'zebra': True}]]
>>>
>>> con.close()JSON can always be sent in serialized form to the server:>>> import json
>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>>
>>> val = ['Apollo 11 Cave', True, 26.003]
>>> con.run("SELECT CAST(:apollo as jsonb)", apollo=json.dumps(val))
[[['Apollo 11 Cave', True, 26.003]]]
>>>
>>> con.close()JSON queries can be have parameters:>>> import pg8000.native
>>>
>>> with pg8000.native.Connection("postgres", password="cpsnow") as con:
...     con.run(""" SELECT CAST('{"a":1, "b":2}' AS jsonb) @> :v """, v={"b": 2})
[[True]]Retrieve Column Metadata From ResultsFind the column metadata returned from a query:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("create temporary table quark (id serial, name text)")
>>> for name in ('Up', 'Down'):
...     con.run("INSERT INTO quark (name) VALUES (:name)", name=name)
>>> # Now execute the query
>>>
>>> con.run("SELECT * FROM quark")
[[1, 'Up'], [2, 'Down']]
>>>
>>> # and retrieve the metadata
>>>
>>> con.columns
[{'table_oid': ..., 'column_attrnum': 1, 'type_oid': 23, 'type_size': 4, 'type_modifier': -1, 'format': 0, 'name': 'id'}, {'table_oid': ..., 'column_attrnum': 2, 'type_oid': 25, 'type_size': -1, 'type_modifier': -1, 'format': 0, 'name': 'name'}]
>>>
>>> # Show just the column names
>>>
>>> [c['name'] for c in con.columns]
['id', 'name']
>>>
>>> con.close()Notices And NotificationsPostgreSQLnoticesare
stored in a deque calledConnection.noticesand added using theappend()method. Similarly there areConnection.notificationsfornotifications. Here’s an example:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("LISTEN aliens_landed")
>>> con.run("NOTIFY aliens_landed")
>>> # A notification is a tuple containing (backend_pid, channel, payload)
>>>
>>> con.notifications[0]
(..., 'aliens_landed', '')
>>>
>>> con.close()Parameter StatusesCertain parameter values are reported by the server automatically at connection startup or whenever
their values changeand pg8000
stores the latest values in a dict calledConnection.parameter_statuses. Here’s an example where
we set theaplication_nameparameter and then read it from theparameter_statuses:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection(
...     "postgres", password="cpsnow", application_name='AGI')
>>>
>>> con.parameter_statuses['application_name']
'AGI'
>>>
>>> con.close()LIMIT ALLYou might think that the following would work, but in fact it fails:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("SELECT 'silo 1' LIMIT :lim", lim='ALL')
Traceback (most recent call last):
pg8000.exceptions.DatabaseError: ...
>>>
>>> con.close()Instead thedocs saythat
you can sendnullas an alternative toALL, which does work:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("SELECT 'silo 1' LIMIT :lim", lim=None)
[['silo 1']]
>>>
>>> con.close()IN and NOT INYou might think that the following would work, but in fact the server doesn’t like it:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("SELECT 'silo 1' WHERE 'a' IN :v", v=['a', 'b'])
Traceback (most recent call last):
pg8000.exceptions.DatabaseError: ...
>>>
>>> con.close()instead you can write it using theunnestfunction:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run(
...     "SELECT 'silo 1' WHERE 'a' IN (SELECT unnest(CAST(:v as varchar[])))",
...     v=['a', 'b'])
[['silo 1']]
>>> con.close()and you can do the same forNOT IN.Many SQL Statements Can’t Be ParameterizedIn PostgreSQL parameters can only be used fordata values, not identifiers.
Sometimes this might not work as expected, for example the following fails:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> channel = 'top_secret'
>>>
>>> con.run("LISTEN :channel", channel=channel)
Traceback (most recent call last):
pg8000.exceptions.DatabaseError: ...
>>>
>>> con.close()It fails because the PostgreSQL server doesn’t allow this statement to have any
parameters. There are many SQL statements that one might think would have parameters,
but don’t. For these cases the SQL has to be created manually, being careful to use theidentifier()andliteral()functions to escape the values to avoidSQL
injection attacks:>>> from pg8000.native import Connection, identifier, literal
>>>
>>> con = Connection("postgres", password="cpsnow")
>>>
>>> channel = 'top_secret'
>>> payload = 'Aliens Landed!'
>>> con.run(f"LISTEN {identifier(channel)}")
>>> con.run(f"NOTIFY {identifier(channel)}, {literal(payload)}")
>>>
>>> con.notifications[0]
(..., 'top_secret', 'Aliens Landed!')
>>>
>>> con.close()COPY FROM And TO A StreamThe SQLCOPYstatement can be
used to copy from and to a file or file-like object. Here’ an example using the CSV
format:>>> import pg8000.native
>>> from io import StringIO
>>> import csv
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> # Create a CSV file in memory
>>>
>>> stream_in = StringIO()
>>> csv_writer = csv.writer(stream_in)
>>> csv_writer.writerow([1, "electron"])
12
>>> csv_writer.writerow([2, "muon"])
8
>>> csv_writer.writerow([3, "tau"])
7
>>> stream_in.seek(0)
0
>>>
>>> # Create a table and then copy the CSV into it
>>>
>>> con.run("CREATE TEMPORARY TABLE lepton (id SERIAL, name TEXT)")
>>> con.run("COPY lepton FROM STDIN WITH (FORMAT CSV)", stream=stream_in)
>>>
>>> # COPY from a table to a stream
>>>
>>> stream_out = StringIO()
>>> con.run("COPY lepton TO STDOUT WITH (FORMAT CSV)", stream=stream_out)
>>> stream_out.seek(0)
0
>>> for row in csv.reader(stream_out):
...     print(row)
['1', 'electron']
['2', 'muon']
['3', 'tau']
>>>
>>> con.close()It’s also possible to COPY FROM an iterable, which is useful if you’re creating rows
programmatically:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> # Generator function for creating rows
>>> def row_gen():
...     for i, name in ((1, "electron"), (2, "muon"), (3, "tau")):
...         yield f"{i},{name}\n"
>>>
>>> # Create a table and then copy the CSV into it
>>>
>>> con.run("CREATE TEMPORARY TABLE lepton (id SERIAL, name TEXT)")
>>> con.run("COPY lepton FROM STDIN WITH (FORMAT CSV)", stream=row_gen())
>>>
>>> # COPY from a table to a stream
>>>
>>> stream_out = StringIO()
>>> con.run("COPY lepton TO STDOUT WITH (FORMAT CSV)", stream=stream_out)
>>> stream_out.seek(0)
0
>>> for row in csv.reader(stream_out):
...     print(row)
['1', 'electron']
['2', 'muon']
['3', 'tau']
>>>
>>> con.close()Execute Multiple SQL StatementsIf you want to execute a series of SQL statements (eg. an.sqlfile), you can run
them as expected:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> statements = "SELECT 5; SELECT 'Erich Fromm';"
>>>
>>> con.run(statements)
[[5], ['Erich Fromm']]
>>>
>>> con.close()The only caveat is that when executing multiple statements you can’t have any
parameters.Quoted Identifiers in SQLSay you had a column calledMy Column. Since it’s case sensitive and contains a
space, you’d have tosurround it by double quotes.
But you can’t do:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("select 'hello' as "My Column"")
Traceback (most recent call last):
SyntaxError: invalid syntax...
>>>
>>> con.close()since Python uses double quotes to delimit string literals, so one solution is
to use Python’striple quotesto delimit the string
instead:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run('''SELECT 'hello' AS "My Column"''')
[['hello']]
>>>
>>> con.close()another solution, that’s especially useful if the identifier comes from an untrusted
source, is to use theidentifier()function, which correctly quotes and escapes the
identifier as needed:>>> from pg8000.native import Connection, identifier
>>>
>>> con = Connection("postgres", password="cpsnow")
>>>
>>> sql = f"SELECT 'hello' as {identifier('My Column')}"
>>> print(sql)
SELECT 'hello' as "My Column"
>>>
>>> con.run(sql)
[['hello']]
>>>
>>> con.close()this approach guards againstSQL injection attacks. One thing to note if you’re using
explicit schemas (eg.pg_catalog.pg_language) is that the schema name and table name
are both separate identifiers. So to escape them you’d do:>>> from pg8000.native import Connection, identifier
>>>
>>> con = Connection("postgres", password="cpsnow")
>>>
>>> query = (
...     f"SELECT lanname FROM {identifier('pg_catalog')}.{identifier('pg_language')} "
...     f"WHERE lanname = 'sql'"
... )
>>> print(query)
SELECT lanname FROM pg_catalog.pg_language WHERE lanname = 'sql'
>>>
>>> con.run(query)
[['sql']]
>>>
>>> con.close()Custom adapter from a Python type to a PostgreSQL typepg8000 has a mapping from Python types to PostgreSQL types for when it needs to send
SQL parameters to the server. The default mapping that comes with pg8000 is designed to
work well in most cases, but you might want to add or replace the default mapping.A Pythondatetime.timedeltaobject is sent to the server as a PostgreSQLintervaltype,  which has theoid1186. But let’s say we wanted to create our
own Python class to be sent as anintervaltype. Then we’d have to register an
adapter:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> class MyInterval(str):
...     pass
>>>
>>> def my_interval_out(my_interval):
...     return my_interval  # Must return a str
>>>
>>> con.register_out_adapter(MyInterval, my_interval_out)
>>> con.run("SELECT CAST(:interval as interval)", interval=MyInterval("2 hours"))
[[datetime.timedelta(seconds=7200)]]
>>>
>>> con.close()Note that it still came back as adatetime.timedeltaobject because we only changed
the mapping from Python to PostgreSQL. See below for an example of how to change the
mapping from PostgreSQL to Python.Custom adapter from a PostgreSQL type to a Python typepg8000 has a mapping from PostgreSQL types to Python types for when it receives SQL
results from the server. The default mapping that comes with pg8000 is designed to work
well in most cases, but you might want to add or replace the default mapping.If pg8000 receives PostgreSQLintervaltype, which has theoid1186, it converts
it into a Pythondatetime.timedeltaobject. But let’s say we wanted to create our
own Python class to be used instead ofdatetime.timedelta. Then we’d have to
register an adapter:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> class MyInterval(str):
...     pass
>>>
>>> def my_interval_in(my_interval_str):  # The parameter is of type str
...     return MyInterval(my_interval)
>>>
>>> con.register_in_adapter(1186, my_interval_in)
>>> con.run("SELECT \'2 years'")
[['2 years']]
>>>
>>> con.close()Note that registering the ‘in’ adapter only afects the mapping from the PostgreSQL type
to the Python type. See above for an example of how to change the mapping from
PostgreSQL to Python.Could Not Determine Data Type Of ParameterSometimes you’ll get the ‘could not determine data type of parameter’ error message from
the server:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("SELECT :v IS NULL", v=None)
Traceback (most recent call last):
pg8000.exceptions.DatabaseError: {'S': 'ERROR', 'V': 'ERROR', 'C': '42P18', 'M': 'could not determine data type of parameter $1', 'F': 'postgres.c', 'L': '...', 'R': '...'}
>>>
>>> con.close()One way of solving it is to put aCASTin the SQL:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("SELECT cast(:v as TIMESTAMP) IS NULL", v=None)
[[True]]
>>>
>>> con.close()Another way is to override the type that pg8000 sends along with each parameter:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> con.run("SELECT :v IS NULL", v=None, types={'v': pg8000.native.TIMESTAMP})
[[True]]
>>>
>>> con.close()Prepared StatementsPrepared statementscan be useful in improving performance when you have a statement that’s executed
repeatedly. Here’s an example:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection("postgres", password="cpsnow")
>>>
>>> # Create the prepared statement
>>> ps = con.prepare("SELECT cast(:v as varchar)")
>>>
>>> # Execute the statement repeatedly
>>> ps.run(v="speedy")
[['speedy']]
>>> ps.run(v="rapid")
[['rapid']]
>>> ps.run(v="swift")
[['swift']]
>>>
>>> # Close the prepared statement, releasing resources on the server
>>> ps.close()
>>>
>>> con.close()Use Environment Variables As Connection DefaultsYou might want to use the current user as the database username for example:>>> import pg8000.native
>>> import getpass
>>>
>>> # Connect to the database with current user name
>>> username = getpass.getuser()
>>> connection = pg8000.native.Connection(username, password="cpsnow")
>>>
>>> connection.run("SELECT 'pilau'")
[['pilau']]
>>>
>>> connection.close()or perhaps you may want to use some of the sameenvironment variables that libpg uses:>>> import pg8000.native
>>> from os import environ
>>>
>>> username = environ.get('PGUSER', 'postgres')
>>> password = environ.get('PGPASSWORD', 'cpsnow')
>>> host = environ.get('PGHOST', 'localhost')
>>> port = environ.get('PGPORT', '5432')
>>> database = environ.get('PGDATABASE')
>>>
>>> connection = pg8000.native.Connection(
...     username, password=password, host=host, port=port, database=database)
>>>
>>> connection.run("SELECT 'Mr Cairo'")
[['Mr Cairo']]
>>>
>>> connection.close()It might be asked, why doesn’t pg8000 have this behaviour built in? The thinking
follows the second aphorism ofThe Zen of Python:Explicit is better than implicit.So we’ve taken the approach of only being able to set connection parameters using thepg8000.native.Connection()constructor.Connect To PostgreSQL Over SSLTo connect to the server using SSL defaults do:import pg8000.native
connection = pg8000.native.Connection('postgres', password="cpsnow", ssl_context=True)
connection.run("SELECT 'The game is afoot!'")To connect over SSL with custom settings, set thessl_contextparameter to anssl.SSLContextobject:import pg8000.native
import ssl


ssl_context = ssl.create_default_context()
ssl_context.verify_mode = ssl.CERT_REQUIRED
ssl_context.load_verify_locations('root.pem')
connection = pg8000.native.Connection(
  'postgres', password="cpsnow", ssl_context=ssl_context)It may be that your PostgreSQL server is behind an SSL proxy server in which case you
can set a pg8000-specific attributessl.SSLContext.request_ssl = Falsewhich tells
pg8000 to connect using an SSL socket, but not to request SSL from the PostgreSQL
server:import pg8000.native
import ssl

ssl_context = ssl.create_default_context()
ssl_context.request_ssl = False
connection = pg8000.native.Connection(
    'postgres', password="cpsnow", ssl_context=ssl_context)Server-Side CursorsYou can use the SQL commandsDECLARE,FETCH,MOVEandCLOSEto manipulate
server-side cursors. For example:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection('postgres', password="cpsnow")
>>> con.run("START TRANSACTION")
>>> con.run("DECLARE c SCROLL CURSOR FOR SELECT * FROM generate_series(1, 100)")
>>> con.run("FETCH FORWARD 5 FROM c")
[[1], [2], [3], [4], [5]]
>>> con.run("MOVE FORWARD 50 FROM c")
>>> con.run("FETCH BACKWARD 10 FROM c")
[[54], [53], [52], [51], [50], [49], [48], [47], [46], [45]]
>>> con.run("CLOSE c")
>>> con.run("ROLLBACK")
>>>
>>> con.close()BLOBs (Binary Large Objects)There’s a set ofSQL functionsfor manipulating BLOBs.
Here’s an example:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection('postgres', password="cpsnow")
>>>
>>> # Create a BLOB and get its oid
>>> data = b'hello'
>>> res = con.run("SELECT lo_from_bytea(0, :data)", data=data)
>>> oid = res[0][0]
>>>
>>> # Create a table and store the oid of the BLOB
>>> con.run("CREATE TEMPORARY TABLE image (raster oid)")
>>>
>>> con.run("INSERT INTO image (raster) VALUES (:oid)", oid=oid)
>>> # Retrieve the data using the oid
>>> con.run("SELECT lo_get(:oid)", oid=oid)
[[b'hello']]
>>>
>>> # Add some data to the end of the BLOB
>>> more_data = b' all'
>>> offset = len(data)
>>> con.run(
...     "SELECT lo_put(:oid, :offset, :data)",
...     oid=oid, offset=offset, data=more_data)
[['']]
>>> con.run("SELECT lo_get(:oid)", oid=oid)
[[b'hello all']]
>>>
>>> # Download a part of the data
>>> con.run("SELECT lo_get(:oid, 6, 3)", oid=oid)
[[b'all']]
>>>
>>> con.close()Replication ProtocolThe PostgreSQLReplication Protocolis supported using
thereplicationkeyword when creating a connection:>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection(
...    'postgres', password="cpsnow", replication="database")
>>>
>>> con.run("IDENTIFY_SYSTEM")
[['...', 1, '.../...', 'postgres']]
>>>
>>> con.close()DB-API 2 Interactive ExamplesThese examples stick to the DB-API 2.0 standard.Basic ExampleImport pg8000, connect to the database, create a table, add some rows and then query the
table:>>> import pg8000.dbapi
>>>
>>> conn = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> cursor = conn.cursor()
>>> cursor.execute("CREATE TEMPORARY TABLE book (id SERIAL, title TEXT)")
>>> cursor.execute(
...     "INSERT INTO book (title) VALUES (%s), (%s) RETURNING id, title",
...     ("Ender's Game", "Speaker for the Dead"))
>>> results = cursor.fetchall()
>>> for row in results:
...     id, title = row
...     print("id = %s, title = %s" % (id, title))
id = 1, title = Ender's Game
id = 2, title = Speaker for the Dead
>>> conn.commit()
>>>
>>> conn.close()Query Using FunctionsAnother query, using some PostgreSQL functions:>>> import pg8000.dbapi
>>>
>>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> cursor = con.cursor()
>>>
>>> cursor.execute("SELECT TO_CHAR(TIMESTAMP '2021-10-10', 'YYYY BC')")
>>> cursor.fetchone()
['2021 AD']
>>>
>>> con.close()Interval TypeA query that returns the PostgreSQL interval type:>>> import datetime
>>> import pg8000.dbapi
>>>
>>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> cursor = con.cursor()
>>>
>>> cursor.execute("SELECT timestamp '2013-12-01 16:06' - %s",
... (datetime.date(1980, 4, 27),))
>>> cursor.fetchone()
[datetime.timedelta(days=12271, seconds=57960)]
>>>
>>> con.close()Point TypeA round-trip with aPostgreSQL pointtype:>>> import pg8000.dbapi
>>>
>>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> cursor = con.cursor()
>>>
>>> cursor.execute("SELECT cast(%s as point)", ((2.3,1),))
>>> cursor.fetchone()
[(2.3, 1.0)]
>>>
>>> con.close()Numeric Parameter Stylepg8000 supports all the DB-API parameter styles. Here’s an example of using the
‘numeric’ parameter style:>>> import pg8000.dbapi
>>>
>>> pg8000.dbapi.paramstyle = "numeric"
>>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> cursor = con.cursor()
>>>
>>> cursor.execute("SELECT array_prepend(:1, CAST(:2 AS int[]))", (500, [1, 2, 3, 4],))
>>> cursor.fetchone()
[[500, 1, 2, 3, 4]]
>>> pg8000.dbapi.paramstyle = "format"
>>>
>>> con.close()AutocommitFollowing the DB-API specification, autocommit is off by default. It can be turned on by
using the autocommit property of the connection:>>> import pg8000.dbapi
>>>
>>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> con.autocommit = True
>>>
>>> cur = con.cursor()
>>> cur.execute("vacuum")
>>> conn.autocommit = False
>>> cur.close()
>>>
>>> con.close()Client EncodingWhen communicating with the server, pg8000 uses the character set that the server asks
it to use (the client encoding). By default the client encoding is the database’s
character set (chosen when the database is created), but the client encoding can be
changed in a number of ways (eg. settingCLIENT_ENCODINGinpostgresql.conf).
Another way of changing the client encoding is by using an SQL command. For example:>>> import pg8000.dbapi
>>>
>>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> cur = con.cursor()
>>> cur.execute("SET CLIENT_ENCODING TO 'UTF8'")
>>> cur.execute("SHOW CLIENT_ENCODING")
>>> cur.fetchone()
['UTF8']
>>> cur.close()
>>>
>>> con.close()JSONJSON is sent to the server serialized, and returned de-serialized. Here’s an example:>>> import json
>>> import pg8000.dbapi
>>>
>>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> cur = con.cursor()
>>> val = ['Apollo 11 Cave', True, 26.003]
>>> cur.execute("SELECT cast(%s as json)", (json.dumps(val),))
>>> cur.fetchone()
[['Apollo 11 Cave', True, 26.003]]
>>> cur.close()
>>>
>>> con.close()JSON queries can be have parameters:>>> import pg8000.dbapi
>>>
>>> with pg8000.dbapi.connect("postgres", password="cpsnow") as con:
...     cur = con.cursor()
...     cur.execute(""" SELECT CAST('{"a":1, "b":2}' AS jsonb) @> %s """, ({"b": 2},))
...     for row in cur.fetchall():
...         print(row)
[True]Retrieve Column Names From ResultsUse the columns names retrieved from a query:>>> import pg8000
>>> conn = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> c = conn.cursor()
>>> c.execute("create temporary table quark (id serial, name text)")
>>> c.executemany("INSERT INTO quark (name) VALUES (%s)", (("Up",), ("Down",)))
>>> #
>>> # Now retrieve the results
>>> #
>>> c.execute("select * from quark")
>>> rows = c.fetchall()
>>> keys = [k[0] for k in c.description]
>>> results = [dict(zip(keys, row)) for row in rows]
>>> assert results == [{'id': 1, 'name': 'Up'}, {'id': 2, 'name': 'Down'}]
>>>
>>> conn.close()COPY from and to a fileThe SQLCOPYstatement can
be used to copy from and to a file or file-like object:>>> from io import StringIO
>>> import pg8000.dbapi
>>>
>>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> cur = con.cursor()
>>> #
>>> # COPY from a stream to a table
>>> #
>>> stream_in = StringIO('1\telectron\n2\tmuon\n3\ttau\n')
>>> cur = con.cursor()
>>> cur.execute("create temporary table lepton (id serial, name text)")
>>> cur.execute("COPY lepton FROM stdin", stream=stream_in)
>>> #
>>> # Now COPY from a table to a stream
>>> #
>>> stream_out = StringIO()
>>> cur.execute("copy lepton to stdout", stream=stream_out)
>>> stream_out.getvalue()
'1\telectron\n2\tmuon\n3\ttau\n'
>>>
>>> con.close()Server-Side CursorsYou can use the SQL commandsDECLARE,FETCH,MOVEandCLOSEto manipulate
server-side cursors. For example:>>> import pg8000.dbapi
>>>
>>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> cur = con.cursor()
>>> cur.execute("START TRANSACTION")
>>> cur.execute(
...    "DECLARE c SCROLL CURSOR FOR SELECT * FROM generate_series(1, 100)")
>>> cur.execute("FETCH FORWARD 5 FROM c")
>>> cur.fetchall()
([1], [2], [3], [4], [5])
>>> cur.execute("MOVE FORWARD 50 FROM c")
>>> cur.execute("FETCH BACKWARD 10 FROM c")
>>> cur.fetchall()
([54], [53], [52], [51], [50], [49], [48], [47], [46], [45])
>>> cur.execute("CLOSE c")
>>> cur.execute("ROLLBACK")
>>>
>>> con.close()BLOBs (Binary Large Objects)There’s a set ofSQL functionsfor manipulating BLOBs.
Here’s an example:>>> import pg8000.dbapi
>>>
>>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> cur = con.cursor()
>>>
>>> # Create a BLOB and get its oid
>>> data = b'hello'
>>> cur = con.cursor()
>>> cur.execute("SELECT lo_from_bytea(0, %s)", [data])
>>> oid = cur.fetchone()[0]
>>>
>>> # Create a table and store the oid of the BLOB
>>> cur.execute("CREATE TEMPORARY TABLE image (raster oid)")
>>> cur.execute("INSERT INTO image (raster) VALUES (%s)", [oid])
>>>
>>> # Retrieve the data using the oid
>>> cur.execute("SELECT lo_get(%s)", [oid])
>>> cur.fetchall()
([b'hello'],)
>>>
>>> # Add some data to the end of the BLOB
>>> more_data = b' all'
>>> offset = len(data)
>>> cur.execute("SELECT lo_put(%s, %s, %s)", [oid, offset, more_data])
>>> cur.execute("SELECT lo_get(%s)", [oid])
>>> cur.fetchall()
([b'hello all'],)
>>>
>>> # Download a part of the data
>>> cur.execute("SELECT lo_get(%s, 6, 3)", [oid])
>>> cur.fetchall()
([b'all'],)
>>>
>>> con.close()Type MappingThe following table shows the default mapping between Python types and PostgreSQL types,
and vice versa.If pg8000 doesn’t recognize a type that it receives from PostgreSQL, it will return it
as astrtype. This is how pg8000 handles PostgreSQLenumand XML types. It’s
possible to change the default mapping using adapters (see the examples).Python to PostgreSQL Type MappingPython TypePostgreSQL TypeNotesboolboolintint4strtextfloatfloat8decimal.Decimalnumericbytesbyteadatetime.datetime
(without tzinfo)timestamp
without
timezone+/-infinity PostgreSQL values are
represented as Pythonstrvalues.
If atimestampis too big fordatetime.datetimethen astris
used.datetime.datetime
(with tzinfo)timestamp with
timezone+/-infinity PostgreSQL values are
represented as Pythonstrvalues.
If atimestamptzis too big fordatetime.datetimethen astris
used.datetime.datedate+/-infinity PostgreSQL values are
represented as Pythonstrvalues.
If adateis too big for adatetime.datethen astris
used.datetime.timetime without
time zonedatetime.timedeltaintervalIf anintervalis too big fordatetime.timedeltathen aPGIntervalis used.NoneNULLuuid.UUIDuuidipaddress.IPv4Addressinetipaddress.IPv6Addressinetipaddress.IPv4Networkinetipaddress.IPv6Networkinetintxidlist of intINT4[]list of floatFLOAT8[]list of boolBOOL[]list of strTEXT[]intint2vectorOnly from PostgreSQL to PythonJSONjson, jsonbThe Python JSON is provided as a Python
serialized string. Results returned as
de-serialized JSON.pg8000.Range*rangePostgreSQL multirange types are
represented in Python as a list of
range types.tuplecomposite typeOnly from Python to PostgreSQLTheory Of OperationA concept is tolerated inside the microkernel only if moving it outside the kernel,
i.e., permitting competing implementations, would prevent the implementation of the
system’s required functionality.—Jochen Liedtke, Liedtke’s minimality principlepg8000 is designed to be used with one thread per connection.Pg8000 communicates with the database using thePostgreSQL Frontend/Backend Protocol(FEBE). If a query has no
parameters, pg8000 uses the ‘simple query protocol’. If a query does have parameters,
pg8000 uses the ‘extended query protocol’ with unnamed prepared statements. The steps
for a query with parameters are:Query comes in.Send a PARSE message to the server to create an unnamed prepared statement.Send a BIND message to run against the unnamed prepared statement, resulting in an
unnamed portal on the server.Send an EXECUTE message to read all the results from the portal.It’s also possible to use named prepared statements. In which case the prepared
statement persists on the server, and represented in pg8000 using aPreparedStatementobject. This means that the PARSE step gets executed once up
front, and then only the BIND and EXECUTE steps are repeated subsequently.There are a lot of PostgreSQL data types, but few primitive data types in Python. By
default, pg8000 doesn’t send PostgreSQL data type information in the PARSE step, in
which case PostgreSQL assumes the types implied by the SQL statement. In some cases
PostgreSQL can’t work out a parameter type and so anexplicit castcan be used in the SQL.In the FEBE protocol, each query parameter can be sent to the server either as binary
or text according to the format code. In pg8000 the parameters are always sent as text.Occasionally, the network connection between pg8000 and the server may go down. If
pg8000 encounters a network problem it’ll raise anInterfaceErrorwith the messagenetwork errorand with the original exception set as thecause.Native API DocsNative API DocsDB-API 2 DocsDB-API 2 DocsDesign DecisionsFor theRangetype, the constructor follows thePostgreSQL range constructor functionswhich makes[closed, open)the easiest to express:>>> from pg8000.types import Range
>>>
>>> pg_range = Range(2, 6)TestsInstalltox:pip install toxEnable the PostgreSQL hstore extension by running the SQL command:create extension hstore;Add a line topg_hba.conffor the various authentication options:host    pg8000_md5           all        127.0.0.1/32            md5
host    pg8000_gss           all        127.0.0.1/32            gss
host    pg8000_password      all        127.0.0.1/32            password
host    pg8000_scram_sha_256 all        127.0.0.1/32            scram-sha-256
host    all                  all        127.0.0.1/32            trustSet password encryption toscram-sha-256inpostgresql.conf:password_encryption ='scram-sha-256'Set the password for the postgres user:ALTER USER postgresql WITH PASSWORD 'pw';Runtoxfrom thepg8000directory:toxThis will run the tests against the Python version of the virtual environment, on the
machine, and the installed PostgreSQL version listening on port 5432, or thePGPORTenvironment variable if set.Benchmarks are run as part of the test suite attests/test_benchmarks.py.README.rstThis file is written in thereStructuredTextformat. To generate an
HTML page from it, do:Activate the virtual environment:source venv/bin/activateInstallSphinx:pip install SphinxRunrst2html.py:rst2html.py README.rst README.htmlDoing A Release Of pg8000Runtoxto make sure all tests pass, then update the release notes, then do:git tag -a x.y.z -m "version x.y.z"
rm -r dist
python -m build
twine upload dist/*Release NotesRelease Notes