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: PostgresConnection.IDThe connection’s identifier. The identifier is passed to the connection factory method and must stay attached to the connection at all times. It must not change during the connection’s lifetime.var isClosed: Boolvar logger: LoggerA logger to use for background events.
Instance Methods
func addListener(channel: String, handler: (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: nonisolated(nonsending) (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: nonisolated(nonsending) (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: nonisolated(nonsending) (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(PostgresQuery, logger: Logger, file: String, line: Int, (PostgresRow) throws -> ()) -> EventLoopFuture<PostgresQueryMetadata>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)?, nonisolated(nonsending) (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>
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.