Skip to content
Docs
Modules
pgx

pgx

Module pgx is used to connect to and query PostgreSQL databases.

The core functionality is provided by the pgx (opens in a new tab) library.

Functions

connect

Function signature
connect(dsn string) conn

Connect to the database specified by the dsn string.

Example
>>> conn := pgx.connect("postgres://user:pass@localhost:5432/db")
>>> conn.query("SELECT * FROM users")
[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
>>> conn.close()

Types

conn

The conn object is a connection to a PostgreSQL database.

Methods

exec
Method signature
exec(sql string, args ...object) string

Execute sql by supplying the name of a prepared statement or a SQL string. Arguments may be referenced positionally in the SQL string as $1, $2, etc.

Example
>>> conn.exec("INSERT INTO users (name) VALUES ($1)", "Alice")
query
Method signature
query(sql string, args ...object) list

Runs a query on the server and reads returns a list of records generated by the query.

Example
>>> conn.query("SELECT * FROM users")
[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]
close
Method signature
close()

Close the connection.

Example
>>> conn.close()