postgres/config.rs
1//! Connection configuration.
2
3#![allow(clippy::doc_overindented_list_items)]
4
5use crate::connection::Connection;
6use crate::Client;
7use log::info;
8use std::fmt;
9use std::net::IpAddr;
10use std::path::Path;
11use std::str::FromStr;
12use std::sync::Arc;
13use std::time::Duration;
14use tokio::runtime;
15#[doc(inline)]
16pub use tokio_postgres::config::{
17 ChannelBinding, Host, LoadBalanceHosts, SslMode, SslNegotiation, TargetSessionAttrs,
18};
19use tokio_postgres::error::DbError;
20use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
21use tokio_postgres::{Error, Socket};
22
23/// Connection configuration.
24///
25/// Configuration can be parsed from libpq-style connection strings. These strings come in two formats:
26///
27/// # Key-Value
28///
29/// This format consists of space-separated key-value pairs. Values which are either the empty string or contain
30/// whitespace should be wrapped in `'`. `'` and `\` characters should be backslash-escaped.
31///
32/// ## Keys
33///
34/// * `user` - The username to authenticate with. Defaults to the user executing this process.
35/// * `password` - The password to authenticate with.
36/// * `dbname` - The name of the database to connect to. Defaults to the username.
37/// * `options` - Command line options used to configure the server.
38/// * `application_name` - Sets the `application_name` parameter on the server.
39/// * `sslcert` - Location of the client SSL certificate file.
40/// * `sslkey` - Location for the secret key file used for the client certificate.
41/// * `sslmode` - Controls usage of TLS. If set to `disable`, TLS will not be used. If set to `prefer`, TLS will be used
42/// if available, but not used otherwise. If set to `require`, `verify-ca`, or `verify-full`, TLS will be forced to
43/// be used. Defaults to `prefer`.
44/// * `sslrootcert` - Location of SSL certificate authority (CA) certificate.
45/// * `host` - The host to connect to. On Unix platforms, if the host starts with a `/` character it is treated as the
46/// path to the directory containing Unix domain sockets. Otherwise, it is treated as a hostname. Multiple hosts
47/// can be specified, separated by commas. Each host will be tried in turn when connecting. Required if connecting
48/// with the `connect` method.
49/// * `sslnegotiation` - TLS negotiation method. If set to `direct`, the client will perform direct TLS handshake, this only works for PostgreSQL 17 and newer.
50/// Note that you will need to setup ALPN of TLS client configuration to `postgresql` when using direct TLS.
51/// If set to `postgres`, the default value, it follows original postgres wire protocol to perform the negotiation.
52/// * `hostaddr` - Numeric IP address of host to connect to. This should be in the standard IPv4 address format,
53/// e.g., 172.28.40.9. If your machine supports IPv6, you can also use those addresses.
54/// If this parameter is not specified, the value of `host` will be looked up to find the corresponding IP address,
55/// or if host specifies an IP address, that value will be used directly.
56/// Using `hostaddr` allows the application to avoid a host name look-up, which might be important in applications
57/// with time constraints. However, a host name is required for TLS certificate verification.
58/// Specifically:
59/// * If `hostaddr` is specified without `host`, the value for `hostaddr` gives the server network address.
60/// The connection attempt will fail if the authentication method requires a host name;
61/// * If `host` is specified without `hostaddr`, a host name lookup occurs;
62/// * If both `host` and `hostaddr` are specified, the value for `hostaddr` gives the server network address.
63/// The value for `host` is ignored unless the authentication method requires it,
64/// in which case it will be used as the host name.
65/// * `port` - The port to connect to. Multiple ports can be specified, separated by commas. The number of ports must be
66/// either 1, in which case it will be used for all hosts, or the same as the number of hosts. Defaults to 5432 if
67/// omitted or the empty string.
68/// * `connect_timeout` - The time limit in seconds applied to each socket-level connection attempt. Note that hostnames
69/// can resolve to multiple IP addresses, and this limit is applied to each address. Defaults to no timeout.
70/// * `tcp_user_timeout` - The time limit that transmitted data may remain unacknowledged before a connection is forcibly closed.
71/// This is ignored for Unix domain socket connections. It is only supported on systems where TCP_USER_TIMEOUT is available
72/// and will default to the system default if omitted or set to 0; on other systems, it has no effect.
73/// * `keepalives` - Controls the use of TCP keepalive. A value of 0 disables keepalive and nonzero integers enable it.
74/// This option is ignored when connecting with Unix sockets. Defaults to on.
75/// * `keepalives_idle` - The number of seconds of inactivity after which a keepalive message is sent to the server.
76/// This option is ignored when connecting with Unix sockets. Defaults to 2 hours.
77/// * `keepalives_interval` - The time interval between TCP keepalive probes.
78/// This option is ignored when connecting with Unix sockets.
79/// * `keepalives_retries` - The maximum number of TCP keepalive probes that will be sent before dropping a connection.
80/// This option is ignored when connecting with Unix sockets.
81/// * `target_session_attrs` - Specifies requirements of the session. If set to `read-write`, the client will check that
82/// the `transaction_read_write` session parameter is set to `on`. This can be used to connect to the primary server
83/// in a database cluster as opposed to the secondary read-only mirrors. Defaults to `all`.
84/// * `channel_binding` - Controls usage of channel binding in the authentication process. If set to `disable`, channel
85/// binding will not be used. If set to `prefer`, channel binding will be used if available, but not used otherwise.
86/// If set to `require`, the authentication process will fail if channel binding is not used. Defaults to `prefer`.
87/// * `load_balance_hosts` - Controls the order in which the client tries to connect to the available hosts and
88/// addresses. Once a connection attempt is successful no other hosts and addresses will be tried. This parameter
89/// is typically used in combination with multiple host names or a DNS record that returns multiple IPs. If set to
90/// `disable`, hosts and addresses will be tried in the order provided. If set to `random`, hosts will be tried
91/// in a random order, and the IP addresses resolved from a hostname will also be tried in a random order. Defaults
92/// to `disable`.
93///
94/// ## Examples
95///
96/// ```not_rust
97/// host=localhost user=postgres connect_timeout=10 keepalives=0
98/// ```
99///
100/// ```not_rust
101/// host=/var/run/postgresql,localhost port=1234 user=postgres password='password with spaces'
102/// ```
103///
104/// ```not_rust
105/// host=host1,host2,host3 port=1234,,5678 hostaddr=127.0.0.1,127.0.0.2,127.0.0.3 user=postgres target_session_attrs=read-write
106/// ```
107///
108/// ```not_rust
109/// host=host1,host2,host3 port=1234,,5678 user=postgres target_session_attrs=read-write
110/// ```
111///
112/// # Url
113///
114/// This format resembles a URL with a scheme of either `postgres://` or `postgresql://`. All components are optional,
115/// and the format accepts query parameters for all of the key-value pairs described in the section above. Multiple
116/// host/port pairs can be comma-separated. Unix socket paths in the host section of the URL should be percent-encoded,
117/// as the path component of the URL specifies the database name.
118///
119/// ## Examples
120///
121/// ```not_rust
122/// postgresql://user@localhost
123/// ```
124///
125/// ```not_rust
126/// postgresql://user:password@%2Fvar%2Frun%2Fpostgresql/mydb?connect_timeout=10
127/// ```
128///
129/// ```not_rust
130/// postgresql://user@host1:1234,host2,host3:5678?target_session_attrs=read-write
131/// ```
132///
133/// ```not_rust
134/// postgresql:///mydb?user=user&host=/var/run/postgresql
135/// ```
136#[derive(Clone)]
137pub struct Config {
138 config: tokio_postgres::Config,
139 notice_callback: Arc<dyn Fn(DbError) + Send + Sync>,
140}
141
142impl fmt::Debug for Config {
143 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
144 fmt.debug_struct("Config")
145 .field("config", &self.config)
146 .finish()
147 }
148}
149
150impl Default for Config {
151 fn default() -> Config {
152 Config::new()
153 }
154}
155
156impl Config {
157 /// Creates a new configuration.
158 pub fn new() -> Config {
159 tokio_postgres::Config::new().into()
160 }
161
162 /// Sets the user to authenticate with.
163 ///
164 /// If the user is not set, then this defaults to the user executing this process.
165 pub fn user(&mut self, user: &str) -> &mut Config {
166 self.config.user(user);
167 self
168 }
169
170 /// Gets the user to authenticate with, if one has been configured with
171 /// the `user` method.
172 pub fn get_user(&self) -> Option<&str> {
173 self.config.get_user()
174 }
175
176 /// Sets the password to authenticate with.
177 pub fn password<T>(&mut self, password: T) -> &mut Config
178 where
179 T: AsRef<[u8]>,
180 {
181 self.config.password(password);
182 self
183 }
184
185 /// Gets the password to authenticate with, if one has been configured with
186 /// the `password` method.
187 pub fn get_password(&self) -> Option<&[u8]> {
188 self.config.get_password()
189 }
190
191 /// Sets the name of the database to connect to.
192 ///
193 /// Defaults to the user.
194 pub fn dbname(&mut self, dbname: &str) -> &mut Config {
195 self.config.dbname(dbname);
196 self
197 }
198
199 /// Gets the name of the database to connect to, if one has been configured
200 /// with the `dbname` method.
201 pub fn get_dbname(&self) -> Option<&str> {
202 self.config.get_dbname()
203 }
204
205 /// Sets command line options used to configure the server.
206 pub fn options(&mut self, options: &str) -> &mut Config {
207 self.config.options(options);
208 self
209 }
210
211 /// Gets the command line options used to configure the server, if the
212 /// options have been set with the `options` method.
213 pub fn get_options(&self) -> Option<&str> {
214 self.config.get_options()
215 }
216
217 /// Sets the value of the `application_name` runtime parameter.
218 pub fn application_name(&mut self, application_name: &str) -> &mut Config {
219 self.config.application_name(application_name);
220 self
221 }
222
223 /// Gets the value of the `application_name` runtime parameter, if it has
224 /// been set with the `application_name` method.
225 pub fn get_application_name(&self) -> Option<&str> {
226 self.config.get_application_name()
227 }
228
229 /// Sets the client SSL certificate in PEM format.
230 ///
231 /// Defaults to `None`.
232 pub fn ssl_cert(&mut self, ssl_cert: &[u8]) -> &mut Config {
233 self.config.ssl_cert(ssl_cert);
234 self
235 }
236
237 /// Gets the location of the client SSL certificate in PEM format.
238 pub fn get_ssl_cert(&self) -> Option<&[u8]> {
239 self.config.get_ssl_cert()
240 }
241
242 /// Sets the client SSL key in PEM format.
243 ///
244 /// Defaults to `None`.
245 pub fn ssl_key(&mut self, ssl_key: &[u8]) -> &mut Config {
246 self.config.ssl_key(ssl_key);
247 self
248 }
249
250 /// Gets the client SSL key in PEM format.
251 pub fn get_ssl_key(&self) -> Option<&[u8]> {
252 self.config.get_ssl_key()
253 }
254
255 /// Sets the SSL configuration.
256 ///
257 /// Defaults to `prefer`.
258 pub fn ssl_mode(&mut self, ssl_mode: SslMode) -> &mut Config {
259 self.config.ssl_mode(ssl_mode);
260 self
261 }
262
263 /// Gets the SSL configuration.
264 pub fn get_ssl_mode(&self) -> SslMode {
265 self.config.get_ssl_mode()
266 }
267
268 /// Sets the SSL certificate authority (CA) certificate in PEM format.
269 ///
270 /// Defaults to `None`.
271 pub fn ssl_root_cert(&mut self, ssl_root_cert: &[u8]) -> &mut Config {
272 self.config.ssl_root_cert(ssl_root_cert);
273 self
274 }
275
276 /// Gets the SSL certificate authority (CA) certificate in PEM format.
277 pub fn get_ssl_root_cert(&self) -> Option<&[u8]> {
278 self.config.get_ssl_root_cert()
279 }
280
281 /// Sets the SSL negotiation method
282 pub fn ssl_negotiation(&mut self, ssl_negotiation: SslNegotiation) -> &mut Config {
283 self.config.ssl_negotiation(ssl_negotiation);
284 self
285 }
286
287 /// Gets the SSL negotiation method
288 pub fn get_ssl_negotiation(&self) -> SslNegotiation {
289 self.config.get_ssl_negotiation()
290 }
291
292 /// Adds a host to the configuration.
293 ///
294 /// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix
295 /// systems, a host starting with a `/` is interpreted as a path to a directory containing Unix domain sockets.
296 /// There must be either no hosts, or the same number of hosts as hostaddrs.
297 pub fn host(&mut self, host: &str) -> &mut Config {
298 self.config.host(host);
299 self
300 }
301
302 /// Gets the hosts that have been added to the configuration with `host`.
303 pub fn get_hosts(&self) -> &[Host] {
304 self.config.get_hosts()
305 }
306
307 /// Gets the hostaddrs that have been added to the configuration with `hostaddr`.
308 pub fn get_hostaddrs(&self) -> &[IpAddr] {
309 self.config.get_hostaddrs()
310 }
311
312 /// Adds a Unix socket host to the configuration.
313 ///
314 /// Unlike `host`, this method allows non-UTF8 paths.
315 #[cfg(unix)]
316 pub fn host_path<T>(&mut self, host: T) -> &mut Config
317 where
318 T: AsRef<Path>,
319 {
320 self.config.host_path(host);
321 self
322 }
323
324 /// Adds a hostaddr to the configuration.
325 ///
326 /// Multiple hostaddrs can be specified by calling this method multiple times, and each will be tried in order.
327 /// There must be either no hostaddrs, or the same number of hostaddrs as hosts.
328 pub fn hostaddr(&mut self, hostaddr: IpAddr) -> &mut Config {
329 self.config.hostaddr(hostaddr);
330 self
331 }
332
333 /// Adds a port to the configuration.
334 ///
335 /// Multiple ports can be specified by calling this method multiple times. There must either be no ports, in which
336 /// case the default of 5432 is used, a single port, in which it is used for all hosts, or the same number of ports
337 /// as hosts.
338 pub fn port(&mut self, port: u16) -> &mut Config {
339 self.config.port(port);
340 self
341 }
342
343 /// Gets the ports that have been added to the configuration with `port`.
344 pub fn get_ports(&self) -> &[u16] {
345 self.config.get_ports()
346 }
347
348 /// Sets the timeout applied to socket-level connection attempts.
349 ///
350 /// Note that hostnames can resolve to multiple IP addresses, and this timeout will apply to each address of each
351 /// host separately. Defaults to no limit.
352 pub fn connect_timeout(&mut self, connect_timeout: Duration) -> &mut Config {
353 self.config.connect_timeout(connect_timeout);
354 self
355 }
356
357 /// Gets the connection timeout, if one has been set with the
358 /// `connect_timeout` method.
359 pub fn get_connect_timeout(&self) -> Option<&Duration> {
360 self.config.get_connect_timeout()
361 }
362
363 /// Sets the TCP user timeout.
364 ///
365 /// This is ignored for Unix domain socket connections. It is only supported on systems where
366 /// TCP_USER_TIMEOUT is available and will default to the system default if omitted or set to 0;
367 /// on other systems, it has no effect.
368 pub fn tcp_user_timeout(&mut self, tcp_user_timeout: Duration) -> &mut Config {
369 self.config.tcp_user_timeout(tcp_user_timeout);
370 self
371 }
372
373 /// Gets the TCP user timeout, if one has been set with the
374 /// `user_timeout` method.
375 pub fn get_tcp_user_timeout(&self) -> Option<&Duration> {
376 self.config.get_tcp_user_timeout()
377 }
378
379 /// Controls the use of TCP keepalive.
380 ///
381 /// This is ignored for Unix domain socket connections. Defaults to `true`.
382 pub fn keepalives(&mut self, keepalives: bool) -> &mut Config {
383 self.config.keepalives(keepalives);
384 self
385 }
386
387 /// Reports whether TCP keepalives will be used.
388 pub fn get_keepalives(&self) -> bool {
389 self.config.get_keepalives()
390 }
391
392 /// Sets the amount of idle time before a keepalive packet is sent on the connection.
393 ///
394 /// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled. Defaults to 2 hours.
395 pub fn keepalives_idle(&mut self, keepalives_idle: Duration) -> &mut Config {
396 self.config.keepalives_idle(keepalives_idle);
397 self
398 }
399
400 /// Gets the configured amount of idle time before a keepalive packet will
401 /// be sent on the connection.
402 pub fn get_keepalives_idle(&self) -> Duration {
403 self.config.get_keepalives_idle()
404 }
405
406 /// Sets the time interval between TCP keepalive probes.
407 /// On Windows, this sets the value of the tcp_keepalive struct’s keepaliveinterval field.
408 ///
409 /// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled.
410 pub fn keepalives_interval(&mut self, keepalives_interval: Duration) -> &mut Config {
411 self.config.keepalives_interval(keepalives_interval);
412 self
413 }
414
415 /// Gets the time interval between TCP keepalive probes.
416 pub fn get_keepalives_interval(&self) -> Option<Duration> {
417 self.config.get_keepalives_interval()
418 }
419
420 /// Sets the maximum number of TCP keepalive probes that will be sent before dropping a connection.
421 ///
422 /// This is ignored for Unix domain sockets, or if the `keepalives` option is disabled.
423 pub fn keepalives_retries(&mut self, keepalives_retries: u32) -> &mut Config {
424 self.config.keepalives_retries(keepalives_retries);
425 self
426 }
427
428 /// Gets the maximum number of TCP keepalive probes that will be sent before dropping a connection.
429 pub fn get_keepalives_retries(&self) -> Option<u32> {
430 self.config.get_keepalives_retries()
431 }
432
433 /// Sets the requirements of the session.
434 ///
435 /// This can be used to connect to the primary server in a clustered database rather than one of the read-only
436 /// secondary servers. Defaults to `Any`.
437 pub fn target_session_attrs(
438 &mut self,
439 target_session_attrs: TargetSessionAttrs,
440 ) -> &mut Config {
441 self.config.target_session_attrs(target_session_attrs);
442 self
443 }
444
445 /// Gets the requirements of the session.
446 pub fn get_target_session_attrs(&self) -> TargetSessionAttrs {
447 self.config.get_target_session_attrs()
448 }
449
450 /// Sets the channel binding behavior.
451 ///
452 /// Defaults to `prefer`.
453 pub fn channel_binding(&mut self, channel_binding: ChannelBinding) -> &mut Config {
454 self.config.channel_binding(channel_binding);
455 self
456 }
457
458 /// Gets the channel binding behavior.
459 pub fn get_channel_binding(&self) -> ChannelBinding {
460 self.config.get_channel_binding()
461 }
462
463 /// Sets the host load balancing behavior.
464 ///
465 /// Defaults to `disable`.
466 pub fn load_balance_hosts(&mut self, load_balance_hosts: LoadBalanceHosts) -> &mut Config {
467 self.config.load_balance_hosts(load_balance_hosts);
468 self
469 }
470
471 /// Gets the host load balancing behavior.
472 pub fn get_load_balance_hosts(&self) -> LoadBalanceHosts {
473 self.config.get_load_balance_hosts()
474 }
475
476 /// Sets the notice callback.
477 ///
478 /// This callback will be invoked with the contents of every
479 /// [`AsyncMessage::Notice`] that is received by the connection. Notices use
480 /// the same structure as errors, but they are not "errors" per-se.
481 ///
482 /// Notices are distinct from notifications, which are instead accessible
483 /// via the [`Notifications`] API.
484 ///
485 /// [`AsyncMessage::Notice`]: tokio_postgres::AsyncMessage::Notice
486 /// [`Notifications`]: crate::Notifications
487 pub fn notice_callback<F>(&mut self, f: F) -> &mut Config
488 where
489 F: Fn(DbError) + Send + Sync + 'static,
490 {
491 self.notice_callback = Arc::new(f);
492 self
493 }
494
495 /// Opens a connection to a PostgreSQL database.
496 pub fn connect<T>(&self, tls: T) -> Result<Client, Error>
497 where
498 T: MakeTlsConnect<Socket> + 'static + Send,
499 T::TlsConnect: Send,
500 T::Stream: Send,
501 <T::TlsConnect as TlsConnect<Socket>>::Future: Send,
502 {
503 let runtime = runtime::Builder::new_current_thread()
504 .enable_all()
505 .build()
506 .unwrap(); // FIXME don't unwrap
507
508 let (client, connection) = runtime.block_on(self.config.connect(tls))?;
509
510 let connection = Connection::new(runtime, connection, self.notice_callback.clone());
511 Ok(Client::new(connection, client))
512 }
513}
514
515impl FromStr for Config {
516 type Err = Error;
517
518 fn from_str(s: &str) -> Result<Config, Error> {
519 s.parse::<tokio_postgres::Config>().map(Config::from)
520 }
521}
522
523impl From<tokio_postgres::Config> for Config {
524 fn from(config: tokio_postgres::Config) -> Config {
525 Config {
526 config,
527 notice_callback: Arc::new(|notice| {
528 info!("{}: {}", notice.severity(), notice.message())
529 }),
530 }
531 }
532}