postgleam/config
Connection configuration for PostgreSQL.
Build a config with default() and the builder functions, or parse a
connection URL with from_url:
let assert Ok(cfg) =
config.from_url("postgres://app:secret@db.internal:5432/app?sslmode=verify-full")
Types
Authentication methods a server may ask for.
pub type AuthMethod {
AuthNone
AuthScram
AuthMd5
AuthCleartext
}
Constructors
-
AuthNoneThe server accepts the connection without a password (trust auth).
-
AuthScramSCRAM-SHA-256 (recommended; mutual authentication).
-
AuthMd5MD5 password hash (legacy; vulnerable to replay with a stolen hash).
-
AuthCleartextCleartext password. Refused on non-TLS connections even when allowed.
A TLS client certificate and its private key, both PEM file paths.
pub type ClientCert {
ClientCert(certfile: String, keyfile: String)
}
Constructors
-
ClientCert(certfile: String, keyfile: String)
Connection configuration.
The password is held in an opaque Secret so that printing a config
(with echo or string.inspect) does not reveal it.
pub type Config {
Config(
host: String,
port: Int,
database: String,
username: String,
password: Secret,
timeout: Int,
connect_timeout: Int,
extra_parameters: List(#(String, String)),
ssl: SslMode,
ssl_ca_file: option.Option(String),
ssl_client_cert: option.Option(ClientCert),
allowed_auth: List(AuthMethod),
pgbouncer: Bool,
idle_interval: Int,
queue_timeout: Int,
max_message_size: Int,
)
}
Constructors
-
Config( host: String, port: Int, database: String, username: String, password: Secret, timeout: Int, connect_timeout: Int, extra_parameters: List(#(String, String)), ssl: SslMode, ssl_ca_file: option.Option(String), ssl_client_cert: option.Option(ClientCert), allowed_auth: List(AuthMethod), pgbouncer: Bool, idle_interval: Int, queue_timeout: Int, max_message_size: Int, )Arguments
- timeout
-
Milliseconds to wait for each response from the server. A query that exceeds it is cancelled on the server and returns
TimeoutError. - connect_timeout
-
Milliseconds to wait for the TCP connection and TLS handshake.
- ssl_ca_file
-
PEM file with the CA certificates used to verify the server (
SslVerifiedonly).Noneuses the operating system’s trust store. - ssl_client_cert
-
PEM certificate and key files for TLS client certificate auth.
- allowed_auth
-
Authentication methods the client will agree to use.
- idle_interval
-
Pool only: idle connections are pinged after this many milliseconds.
- queue_timeout
-
Pool only: how long a caller waits for a free connection.
- max_message_size
-
Largest single protocol message accepted from the server, in bytes.
A secret string (a password). string.inspect shows only //fn() { ... }.
pub opaque type Secret
SSL connection mode.
pub type SslMode {
SslDisabled
SslVerified
SslUnverified
}
Constructors
-
SslDisabledNo TLS (plain TCP). Only for local development.
-
SslVerifiedTLS required; the server certificate and host name are verified.
-
SslUnverifiedTLS required, but the certificate is NOT verified. Protects against passive sniffing only; an active attacker can impersonate the server.
Values
pub fn allowed_auth(
config: Config,
methods: List(AuthMethod),
) -> Config
Restrict which authentication methods the client will use, like libpq’s
require_auth. For example [AuthScram] refuses MD5, cleartext and
passwordless servers.
pub fn default() -> Config
Default config for a local server: postgres@localhost:5432/postgres,
no password, no TLS. Set a password with password.
pub fn extra_parameters(
config: Config,
params: List(#(String, String)),
) -> Config
Set extra startup parameters sent to PostgreSQL (e.g. application_name).
pub fn from_url(url: String) -> Result(Config, UrlError)
Parse a connection URL such as
postgres://user:password@host:5432/database?sslmode=verify-full.
Supported query parameters: sslmode (disable, require, verify-ca,
verify-full), sslrootcert, connect_timeout (seconds) and
application_name. Unknown parameters are sent to the server as startup
parameters.
pub fn idle_interval(config: Config, ms: Int) -> Config
Set the idle ping interval in milliseconds (pool only). Connections idle for longer than this are pinged. Default: 1000ms.
pub fn max_message_size(config: Config, bytes: Int) -> Config
Set the largest protocol message accepted from the server. Default 1 GiB.
pub fn pgbouncer(config: Config, enabled: Bool) -> Config
Enable PgBouncer compatibility mode. Uses unnamed prepared statements with Flush to avoid mid-query backend reassignment in PgBouncer transaction pooling mode.
pub fn queue_timeout(config: Config, ms: Int) -> Config
Set the maximum time in milliseconds to wait for a pool connection. If no connection becomes available within this time, returns TimeoutError. Default: 5000ms.
pub fn ssl_ca_file(config: Config, path: String) -> Config
Verify the server against the CA certificates in this PEM file instead of the system trust store (for RDS, Cloud SQL or a private CA).
pub fn ssl_client_cert(
config: Config,
certfile certfile: String,
keyfile keyfile: String,
) -> Config
Authenticate with a TLS client certificate (PEM certificate and key files).