Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

sql.postgres

import sql.postgres

sql does not re-export this module, so it is reached only by importing it directly.

The PostgreSQL adapter.

Reach it through sql, which is what keeps a program portable:

import sql

var db = sql.open('postgres://alice:secret@localhost/app')

The module underneath is here for the parts of PostgreSQL that have no equivalent elsewhere: LISTEN and NOTIFY, the server’s own settings, and the type registry, which is how a program teaches the adapter about a type its database defines.

What this adapter speaks

Version 3 of the wire protocol, over TCP, optionally under TLS, or over a unix domain socket. A host beginning with a slash is a socket directory, as libpq spells it, and the socket inside it is named after the port. TLS is neither offered nor wanted there, since nothing sits in between, so sslmode is ignored.

Statements go through the extended protocol, which compiles them on the server and binds values rather than substituting them. Values travel in the server’s own binary form wherever this adapter has a codec for the type, and as text otherwise, so a type it has never heard of still arrives readable.

Submodules

ModuleReached asSummary
sql.postgres.authimport sql.postgres.authThe authentication exchanges a PostgreSQL server can ask for.
sql.postgres.connectionsql.postgres.connection.*A PostgreSQL connection, as the layer above the adapters sees one.
sql.postgres.cursorsql.postgres.cursor.*Reading a PostgreSQL result a batch at a time.
sql.postgres.driversql.postgres.driver.*Opening PostgreSQL connections: reading a connection string, establishing the socket, upgrading it to TLS…
sql.postgres.errorssql.postgres.errors.*Turning PostgreSQL’s SQLSTATE codes into the shared error classes.
sql.postgres.messagesimport sql.postgres.messagesFraming for the PostgreSQL wire protocol, version 3.
sql.postgres.notifysql.postgres.notify.*LISTEN and NOTIFY, PostgreSQL’s own publish and subscribe.
sql.postgres.protocolsql.postgres.protocol.*The PostgreSQL frontend/backend protocol, version 3.
sql.postgres.resultssql.postgres.results.*Reading what the server says about a result it has just produced.
sql.postgres.schemaimport sql.postgres.schemaIntrospection for PostgreSQL, through the standard information_schema views and the catalogue behind them.
sql.postgres.statementsql.postgres.statement.*A PostgreSQL statement compiled once and run many times.
sql.postgres.typessql.postgres.types.*Encoding and decoding PostgreSQL’s binary value formats.

Constants

DRIVER

sql.postgres.DRIVER

The driver instance sql registers for this engine.

Functions

connect()

sql.postgres.connect(dsn) -> PostgresConnection

Opens a PostgreSQL connection directly, without going through sql.

Anything meant to stay portable should use sql.open() instead.

Parameters

  • dsn (string|dict) — A connection string or options.

Returns PostgresConnection


2026, Richard Ore and Zuri contributors