connect

Function connect 

Source
pub async fn connect(connection_string: &str) -> Result<Client>
Expand description

Connect to PostgreSQL database with TLS support

Establishes a connection using the provided connection string with TLS enabled. The connection lifecycle is managed automatically via tokio spawn.

Automatic Keepalive: This function automatically adds TCP keepalive parameters to prevent idle connection timeouts when connecting through load balancers. The following parameters are added if not already present:

  • keepalives=1
  • keepalives_idle=60
  • keepalives_interval=10

§Arguments

  • connection_string - PostgreSQL URL (e.g., “postgresql://user:pass@host:5432/db”)

§Returns

Returns a Client on success, or an error with context if connection fails.

§Errors

This function will return an error if:

  • The connection string format is invalid
  • Authentication fails (invalid username or password)
  • The database does not exist
  • The database server is unreachable
  • TLS negotiation fails
  • Connection times out
  • pg_hba.conf does not allow the connection

§Examples

let client = connect("postgresql://user:pass@localhost:5432/mydb").await?;