Skip to main content

parse

Function parse 

Source
pub fn parse(url: &str) -> Result<Config, ParseError>
Expand description

Parse a PostgreSQL connection URL into a Config.

Supports both postgres:// and postgresql:// schemes.

When the URL does not specify sslmode, it defaults to verify-full to ensure secure connections by default.

§URL Format

Network connections:

postgres://username:password@host:port/database?params

Socket connections (via query params when host starts with / or @):

postgres://?host=/path/to/socket&user=username&dbname=database

Cloud SQL connections (user/password/database in URL, socket path in query):

postgres://username:password@/database?host=/cloudsql/project:region:instance

§Query Parameters

  • sslmode: SSL mode (allow, disable, prefer, require, verify-ca, verify-full)
  • sslrootcert: Path to SSL root certificate or “system”
  • application_name: Application name
  • hostaddr: IP address for the host
  • channel_binding: Channel binding (disable, prefer, require)
  • host: Socket path (when URL has no host component)
  • user: User (when URL has no username component)
  • dbname: Database name (when URL has no path component)
  • password: Password (when URL has no password component)

§Errors

Returns an error if the same parameter is specified both in the URL components and as a query parameter (e.g., password in both places).

§Example

use pg_client::{Config, config::SslMode};

let config = pg_client::url::parse(
    "postgres://user@localhost:5432/mydb",
).unwrap();

assert_eq!(config.session.user.as_str(), "user");
assert_eq!(config.session.database.as_str(), "mydb");
assert_eq!(config.ssl_mode, SslMode::VerifyFull);