PostgresConnection class

A connection to PostgreSQL via package:postgres (pure Dart).

final db = await PostgresConnection.open(
  host: 'localhost', database: 'app', username: 'app', password: '...',
);
DB.use(db);

Transactions run on the driver's transaction session, so the object handed to transaction callbacks is a distinct Connection; use it (User.using(tx)) for every statement that must be inside the transaction. Statements sent to the root connection while a transaction is open wait until it finishes.

Inheritance

Properties

grammar Grammar
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
queryLog List<QueryEvent>
no setterinherited
root → _PostgresSession?
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
session → Session
finalinherited
transactionDepth int
no setteroverride

Methods

close() Future<void>
override
disableQueryLog() → void
inherited
enableQueryLog() → void
Start recording statements into queryLog. Useful in tests to assert how many queries a code path issued.
inherited
execute(String sql, [List<Object?> bindings = const []]) Future<int>
Runs INSERT/UPDATE/DELETE/DDL and returns the number of affected rows.
inherited
flushQueryLog() → void
inherited
insertGetId(String sql, List<Object?> bindings, {String? primaryKey}) Future<Object?>
Runs an INSERT and returns the generated primary key.
inherited
listen(void listener(QueryEvent event)) → void
Observe every statement (for logging, tracing, slow-query alerts).
inherited
logged<R>(String sql, List<Object?> bindings, FutureOr<R> run()) Future<R>
Times run, publishes a QueryEvent, and converts driver errors into QueryException carrying the SQL and bindings.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
select(String sql, [List<Object?> bindings = const []]) Future<List<Row>>
Runs a SELECT and returns every row. Prefer the query builder's chunk()/lazy() for large tables.
inherited
toString() String
A string representation of this object.
inherited
transaction<R>(Future<R> body(Connection tx)) Future<R>
Runs body inside a transaction: commits when it returns, rolls back and rethrows when it throws. Nested calls use savepoints.
override
wrapError(Object error, String sql, List<Object?> bindings) QueryException
Adapters override to map driver errors (unique violations, ...).
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

open({String host = 'localhost', int port = 5432, required String database, String? username, String? password, bool ssl = false, int maxConnections = 5}) Future<PostgresConnection>