Project: YugabyteDB (App Definition and Development / Database)
File: yb-rust-postgres.md

title: YugabyteDB Rust-Postgres Smart Driver headerTitle: Connect an application linkTitle: Connect an app description: Connect a Rust application using YugabyteDB Rust-Postgres Smart Driver for YSQL image: /images/sectionicons/sample-data/ss1-sampledata-3x.png menu: stable: identifier: rust-postgres-driver parent: rust-drivers weight: 600 type: docs <ul class="nav nav-tabs-alt nav-tabs-yb"> <li class="active"> <a href="../yb-rust-postgres/" class="nav-link"> YSQL </a> </li> </ul> <ul class="nav nav-tabs-alt nav-tabs-yb"> <li > <a href="../yb-rust-postgres/" class="nav-link active"> <i class="icon-postgres" aria-hidden="true"></i> YugabyteDB Rust-Postgres Smart Driver </a> </li> </ul> The is a rust driver for built on the , with additional features. {{< note title="YugabyteDB Managed" >}} To use smart driver load balancing features when connecting to clusters in YugabyteDB Managed, applications must be deployed in a VPC that has been peered with the cluster VPC. For applications that access the cluster from outside the VPC network, use the upstream PostgreSQL driver instead; in this case, the cluster performs the load balancing. Applications that use smart drivers from outside the VPC network fall back to the upstream driver behaviour automatically. For more information, refer to . {{< /note >}} The following sections demonstrate how to perform common tasks required for Rust application development using the YugabyteDB Rust smart driver. To start building your application, make sure you have met the . The following table describes the connection parameters required to connect, including for uniform and topology load balancing. | Parameter | Description | Default | | :-- | :- | : | | host | Host name of the YugabyteDB instance. You can also enter . | | | port | Listen port for YSQL | 5433 | | database/dbname | Database name | | | user | User connecting to the database | | | password | User password | | | `load_balance` | | Defaults to upstream driver behavior unless set to 'true' | | `topologykeys` | | If `loadbalance` is true, uses uniform load balancing unless set to comma-separated geo-locations in the form `cloud.region.zone`. | | ybserversrefreshinterval | If loadbalance is true, the interval in seconds to refresh the servers list | 300 seconds| | fallbacktotopologykeysonly | When set to true, if all the servers in the primary and fallback placements are down, the smart driver does not fallback to the rest of the servers in the cluster, instead to the nodes given in the connection URL. | false | | failedhostreconnectdelaysecs | Mark the server as "UP" only if the server is currently present in `yb_servers()` response and `failed-host-reconnect-delay-secs` duration has elapsed from the last time it was marked

| 5 seconds | The following is an example connection string for connecting to YugabyteDB: ```sh postgresql://127.0.0.1:5433/yugabyte?user=yugabyte&password=yugabyte& \ ybserversrefresh_interval=0& \ load_balance=true& \ topology_keys=cloud1.datacenter1.rack2 ``` After the driver establishes the initial connection, it fetches the list of available servers from the cluster, and load-balances subsequent connection requests across these servers. You can specify multiple hosts in the connection string to provide alternative options during the initial connection in case the primary address fails. {{< tip title="Tip">}} To obtain a list of available hosts, you can connect to any cluster node and use the `yb_servers()` YSQL function. {{< /tip >}} Delimit the addresses using commas, as follows: ```sh postgresql://127.0.0.1:5433,127.0.0.2:5433/yugabyte?user=yugabyte&password=yugabyte& \ ybserversrefresh_interval=0& \ load_balance=true& \ topologykeys=cloud1.datacenter1.rack2&fallbacktotopologykeys_only=true ``` The hosts are only used during the initial connection attempt. If the first host is down when the driver is connecting, the driver attempts to connect to the next host in the string, and so on. Make sure that you have created a new rust project as part of the . Add `yb-postgres = "0.19.7-yb-1-beta` dependency in the `Cargo.toml` file as follows: ```toml [package] name = "HelloWorld-rust" version = "0.1.0" edition = "2021" [dependencies] yb-postgres = "0.19.7-yb-1-beta" ``` Replace the existing code in `src/main.rs` with the following sample code to set up tables and query the table contents. Replace the connection string `connection_url` with the cluster credential, if required. ```rust use yb_postgres::{Client, NoTls, Error}; fn main() -> Result<(), Error> { let connectionurl: String = String::from("postgresql://127.0.0.1:5433/yugabyte?user=yugabyte&password=yugabyte&loadbalance=true"); //creating connection to YugabyteDB cluster let mut client=Client::connect(&connection_url,NoTls,)?; println!("Connected to the YugabyteDB Cluster successfully."); client.execute("DROP TABLE IF EXISTS employee", &[])?; client.execute("CREATE TABLE IF NOT EXISTS employee (id int primary key, name varchar, age int, language text)", &[])?; println!("Created table employee"); client.execute("INSERT INTO employee VALUES (1, 'John', 35, 'Java')", &[])?; println!("Inserted employee 1"); client.execute("INSERT INTO employee VALUES (2, 'Sam', 37, 'JavaScript')", &[])?; println!("Inserted employee 2"); println!("Employees Information:"); for row in client.query("select * from employee", &[])? { let id: i32 = row.get(0); let name: String = row.get(1); let age: i32 = row.get(2); let language: String = row.get(3); println!("{}. name = {}, age = {}, language = {}",id, name,age,language); } //closing the connection let _ = client.close(); Ok(()) } ``` Run the `HelloWorld-rust` application using the following command: ```sh cargo run ``` You should see output similar to the following: ```output Created table employee Inserted employee 1 Inserted employee 2 Employees Information: name = John, age = 35, language = Java name = Sam, age = 37, language = JavaScript ``` If there is no output or you get an error, verify the parameters included in the connection string.