Class
PostgresConnection
A Postgres connection. Use it to run queries against a Postgres server.
final class PostgresConnection
Overview
Thread safety is achieved by dispatching all access to shared state onto the underlying EventLoop.
Topics
Structures
struct ConfigurationA configuration object for a connection.
Instance Properties
var closeFuture: EventLoopFuture<Void>var eventLoop: any EventLoopThe underlyingEventLoopof both the connection and its channel.let id: IDvar isClosed: Boolvar logger: LoggerA logger to use for background events.
Instance Methods
func addListener(channel: String, handler: @Sendable (PostgresListenContext, PostgresMessage.NotificationResponse) -> Void) -> PostgresListenContextAdd a handler for NotificationResponse messages on a certain channel. This is used in conjunction with PostgreSQL’sLISTEN/NOTIFYsupport: to listen on a channel, you add a listener using this method to handle the NotificationResponse messages, then issue aLISTENquery to instruct PostgreSQL to begin sending NotificationResponse messages.func authenticate(username: String, database: String?, password: String?, logger: Logger) -> EventLoopFuture<Void>func closeGracefully() async throwsCloses the connection to the server, after all queries that have been created on this connection have been run.func copyFrom(table: String, columns: [String], format: PostgresCopyFromFormat, logger: Logger, file: String, line: Int, writeData: (PostgresCopyFromWriter) async throws -> Void) async throwsCopy data into a table using aCOPY <table name> FROM STDINquery.func copyFromBinary(table: String, columns: [String], options: PostgresCopyFromFormat.BinaryOptions, bufferSize: Int, logger: Logger, file: String, line: Int, writeData: (inout PostgresBinaryCopyFromWriter) async throws -> Void) async throwsCopy data into a table using aCOPY <table name> FROM STDINquery, transferring data in a binary format.func execute(_:logger:file:line:)Execute a prepared statement, taking care of the preparation when necessary.func listen(String) async throws -> PostgresNotificationSequenceStart listening for a channel.func listen<Value>(on: String, consume: (PostgresNotificationSequence) async throws -> Value) async throws -> ValueListen to a channel and run closure withPostgresNotificationSequence.func query(_:logger:file:line:)Run a query on the Postgres server the connection is connected to.func query(_:logger:file:line:_:)Run a query on the Postgres server the connection is connected to and iterate the rows in a callback.func withTransaction<Result>(logger: Logger, file: String, line: Int, isolation: isolated (any Actor)?, (PostgresConnection) async throws -> sending Result) async throws -> sending ResultPuts the connection into an open transaction state, for the providedclosure’s lifetime.
Type Aliases
typealias IDA Postgres connection ID.
Type Properties
static var defaultEventLoopGroup: any EventLoopGroupReturns the defaultEventLoopGroupsingleton, automatically selecting the best for the platform.
Type Methods
static connect(on:configuration:id:logger:)Creates a new connection to a Postgres server.static func connect(to: SocketAddress, tlsConfiguration: TLSConfiguration?, serverHostname: String?, logger: Logger, on: any EventLoop) -> EventLoopFuture<PostgresConnection>static func establishAndAuthChannelWithoutPostgresHandler(using: Configuration, on: any EventLoop, logger: Logger) -> EventLoopFuture<any Channel>Connect to the Postgres server described byconfiguration— negotiate TLS (if configured), send the startup message, and authenticate — then remove PostgresNIO’s handlers, returning the authenticated channel for the caller to drive with its own pipeline. Any TLS handler and the socket are left in place; only PostgresNIO’s own protocol handlers are removed. This lets a caller reuse PostgresNIO’s TCP/TLS/startup/authentication (including SCRAM-SHA-256) and then speak a protocol PostgresNIO does not implement — for example, streaming replication.
Default Implementations
Relationships
Conforms To
PostgresDatabaseSwift.CopyableSwift.EscapableSwift.SendableSwift.SendableMetatype_ConnectionPoolModule.PooledConnection
See Also
Essentials
class PostgresClientA Postgres client that is backed by an underlying connection pool. UsePostgresClient.Configurationto change the client’s behavior.struct Configuration- Running QueriesInteract with the PostgreSQL database by running Queries.