Skip to content

Class

PostgresClient

A Postgres client that is backed by an underlying connection pool. Use PostgresClient.Configuration to change the client’s behavior.
final class PostgresClient

Creating a client

You create a PostgresClient by first creating a PostgresClient.Configuration struct that you can use to modify the client’s behavior.

let config = PostgresClient.Configuration(
    host: "localhost",
    port: 5432,
    username: "my_username",
    password: "my_password",
    database: "my_database",
    tls: .disable
)

Now you can create a client with your configuration object:

let client = PostgresClient(configuration: config)

Running a client

PostgresClient relies on structured concurrency. Because of this it needs a task in which it can schedule all the background work that it needs to do in order to manage connections on the user’s behalf. For this reason, developers must provide a task to the client by scheduling the client’s run method in a long running task:

await withTaskGroup(of: Void.self) { taskGroup in
    taskGroup.addTask {
        await client.run() // !important
    }

    // You can use the client while the `client.run()` method is not cancelled.

    // To shutdown the client, cancel its run method, by cancelling the taskGroup.
    taskGroup.cancelAll()
}

PostgresClient cannot lease connections if its run() method isn’t active. Cancelling the run() method is equivalent to closing the client. Once a client’s run() method has been cancelled, executing queries or prepared statements will fail.

Topics

Structures

Initializers

Instance Methods

Type Properties

  • defaultEventLoopGroup
    Returns the default EventLoopGroup singleton, automatically selecting the best for the platform.

Relationships

Conforms To

  • ServiceLifecycle.Service
  • Swift.Sendable
  • Swift.SendableMetatype

See Also

Essentials