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.
Errors from from_url.
pub type UrlError {
InvalidUrl
UnsupportedScheme(String)
InvalidSslMode(String)
InvalidConnectTimeout(String)
UnsupportedParameter(name: String, value: String)
}
Constructors
-
InvalidUrl -
UnsupportedScheme(String) -
InvalidSslMode(String) -
InvalidConnectTimeout(String) -
UnsupportedParameter(name: String, value: String)The URL asks for something postgleam can’t do, such as
channel_binding=require. Refused rather than ignored, so a URL never promises more than the connection delivers.
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.
Like libpq, a URL without a database connects to the database named after the user.
Query parameters:
sslmode:disable,require,verify-caorverify-full.preferandalloware refused withInvalidSslMode, because postgleam never falls back to an unencrypted connection by itself.sslrootcert,connect_timeout(seconds),application_name.channel_binding:preferanddisableare accepted and change nothing;requireis refused withUnsupportedParameter, because postgleam does not implement SCRAM channel binding.pgbouncer=trueturns on PgBouncer mode (seepgbouncer), andschema=namesetssearch_path, as in Prisma URLs. Prisma’s pool settingsconnection_limitandpool_timeoutare ignored: the pool size is thesizegiven topool.start.
Any other parameter is sent to the server as a startup parameter, so
?statement_timeout=5000 works and a name the server doesn’t know fails
the connection.
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, the call returns
PoolTimeout.
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).