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.schema

import sql.postgres.schema

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

Introspection for PostgreSQL, through the standard information_schema views and the catalogue behind them.

information_schema covers tables, views and columns and is the SQL standard, so those queries would work on other engines too. Constraints and indexes are read from pg_catalog instead, because the standard views describe them in a form that loses which columns an index actually covers and in what order.

Where a caller names no schema, the server’s own search_path decides, which is what makes an unqualified table name mean the same here as it does in a query.

Classes

PostgresSchema

class sql.postgres.schema.PostgresSchema < SchemaAdapter

Answers introspection questions about a PostgreSQL database.

Constructor

sql.postgres.schema.PostgresSchema(connection)

Parameters

  • connection (PostgresConnection)

PostgresSchema.tables()

sql.postgres.schema.PostgresSchema.tables(schema) -> list[string]

The tables the application created.

Parameters

  • schema (string|nil) — The schema to look in, or nil for whatever search_path resolves to.

Returns list[string]

PostgresSchema.views()

sql.postgres.schema.PostgresSchema.views(schema) -> list[string]

The views.

Parameters

  • schema (string|nil)

Returns list[string]

PostgresSchema.columns()

sql.postgres.schema.PostgresSchema.columns(table: string, schema) -> list[dict]

The columns of table, in order.

Parameters

  • table (string)
  • schema (string|nil)

Returns list[dict]

PostgresSchema.primary_key_columns()

sql.postgres.schema.PostgresSchema.primary_key_columns(table: string, schema) -> list[string]

The primary key columns, in key order.

pg_index records the key’s own column order, which is not the table’s, so the order here is the one the index was declared with.

Parameters

  • table (string)
  • schema (string|nil)

Returns list[string]

PostgresSchema.indexes()

sql.postgres.schema.PostgresSchema.indexes(table: string, schema) -> list[dict]

The indexes on table, each { name, columns, unique }.

Parameters

  • table (string)
  • schema (string|nil)

Returns list[dict]

PostgresSchema.foreign_keys()

sql.postgres.schema.PostgresSchema.foreign_keys(table: string, schema) -> list[dict]

The foreign keys declared on table.

Parameters

  • table (string)
  • schema (string|nil)

Returns list[dict]


2026, Richard Ore and Zuri contributors