Skip to main content

parse_pg_conn_str

Function parse_pg_conn_str 

Source
pub fn parse_pg_conn_str(conn_str: &str) -> Result<PgConnParts, OxiSqlError>
Expand description

Parse a PostgreSQL connection string into its constituent parts.

Accepts two common formats:

Key-value format (the libpq/tokio-postgres native format):

host=myhost port=5433 dbname=testdb user=admin password=secret

Unknown keys are silently ignored. Missing host defaults to "localhost"; missing port defaults to 5432.

URI format:

postgres://user:password@host:port/dbname
postgresql://host/dbname

The scheme must be postgres:// or postgresql://.

§Errors

Returns OxiSqlError::Other when:

  • A non-empty port value cannot be parsed as a u16.
  • The URI scheme is unrecognised (neither postgres:// nor postgresql://).

§Examples

use oxisql_postgres::parse_pg_conn_str;

let p = parse_pg_conn_str("host=myhost port=5433 dbname=testdb user=admin").unwrap();
assert_eq!(p.host, "myhost");
assert_eq!(p.port, 5433);
assert_eq!(p.dbname, Some("testdb".to_string()));
assert_eq!(p.user, Some("admin".to_string()));