---
title: "Connect an application"
url: "https://docs.yugabyte.com/stable/develop/drivers-orms/rust/yb-rust-postgres/"
---

# Connect an application

Connect a Rust application using YugabyteDB Rust-Postgres Smart Driver for YSQL

- [YSQL](/stable/develop/drivers-orms/rust/yb-rust-postgres/)

<!--THE END-->

- [YugabyteDB Rust-Postgres Smart Driver](/stable/develop/drivers-orms/rust/yb-rust-postgres/)

The [YugabyteDB Rust-Postgres Smart Driver](https://github.com/yugabyte/rust-postgres "YugabyteDB Rust-Postgres Smart Driver") is a rust driver for [YSQL](/stable/api/ysql/ "YSQL") built on the [PostgreSQL rust-postgres driver](https://github.com/sfackler/rust-postgres "PostgreSQL rust-postgres driver"), with additional [connection load balancing](/stable/develop/drivers-orms/smart-drivers/ "connection load balancing") features.

YugabyteDB Aeon

To use smart driver load balancing features when connecting to clusters in YugabyteDB Aeon, 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 [Using smart drivers with YugabyteDB Aeon](/stable/develop/drivers-orms/smart-drivers/#using-smart-drivers-with-yugabytedb-aeon "Using smart drivers with YugabyteDB Aeon").

## CRUD operations

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 [prerequisites](/stable/develop/drivers-orms/rust/#prerequisites "prerequisites").

### Step 1: Set up the database connection

The following table describes the connection parameters required to connect, including [smart driver parameters](/stable/develop/drivers-orms/smart-drivers/ "smart driver parameters") for uniform and topology load balancing.

| Parameter                            | Description                                                                                                                                                                                                                                      | Default |
|--------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------|
| host                                 | Host name of the YugabyteDB instance. You can also enter [multiple addresses](#use-multiple-addresses "multiple addresses").                                                                                                                     |         |
| port                                 | Listen port for YSQL                                                                                                                                                                                                                             | 5433    |
| database/dbname                      | Database name                                                                                                                                                                                                                                    |         |
| user                                 | User connecting to the database                                                                                                                                                                                                                  |         |
| password                             | User password                                                                                                                                                                                                                                    |         |
| load\_balance                        | Enables [uniform load balancing](/stable/develop/drivers-orms/smart-drivers/#cluster-aware-load-balancing "uniform load balancing")                                                                                                                                     | false   |
| topology\_keys                       | Enables [topology-aware load balancing](/stable/develop/drivers-orms/smart-drivers/#topology-aware-load-balancing "topology-aware load balancing"). Specify comma-separated geo-locations in the form `cloud.region.zone:priority`. Ignored if `load_balance` is false. | Empty   |
| yb\_servers\_refresh\_interval       | The interval (in seconds) to refresh the servers list; ignored if `load_balance` is false                                                                                                                                                        | 300     |
| fallback\_to\_topology\_keys\_only   | If set to true and `topology_keys` are specified, the driver only tries to connect to nodes specified in `topology_keys`                                                                                                                         | false   |
| failed\_host\_reconnect\_delay\_secs | Time (in seconds) to wait before trying to connect to failed nodes. When the driver is unable to connect to a node, it marks the node as failed using a timestamp, and ignores the node when trying new connections until this time elapses.     | 5       |

The following is an example connection string for connecting to YugabyteDB:

```sh
postgresql://127.0.0.1:5433/yugabyte?user=yugabyte&password=yugabyte& \
    yb_servers_refresh_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.

#### Use multiple addresses

You can specify multiple hosts in the connection string to provide alternative options during the initial connection in case the primary address fails.

Tip

To obtain a list of available hosts, you can connect to any cluster node and use the `yb_servers()` YSQL function.

Delimit the addresses using commas, as follows:

```sh
postgresql://127.0.0.1:5433,127.0.0.2:5433/yugabyte?user=yugabyte&password=yugabyte& \
    yb_servers_refresh_interval=0& \
    load_balance=true& \
    topology_keys=cloud1.datacenter1.rack2&fallback_to_topology_keys_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.

### Step 2: Write your application

Make sure that you have created a new rust project as part of the [prerequisites](/stable/develop/drivers-orms/rust/#prerequisites "prerequisites").

1. Add `yb-postgres = "0.19.7-yb-1-beta.3` dependency in the `Cargo.toml` file as follows:
   
   ```toml
   [package]
   name = "HelloWorld-rust"
   version = "0.1.0"
   edition = "2021"
   
   # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
   
   [dependencies]
   yb-postgres = "0.19.7-yb-1-beta.3"
   ```
2. Replace the existing code in `src/main.rs` with the following sample code to set up tables and query the table contents. Replace the values in the connection string `connection_url` with the database credentials, if required.
   
   ```rust
   use yb_postgres::{Client, NoTls, Error};
   fn main() -> Result<(), Error> {
   
       let connection_url: String = String::from("postgresql://127.0.0.1:5433/yugabyte?user=yugabyte&password=yugabyte&load_balance=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(())
   }
   ```
3. 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:
   1. name = John, age = 35, language = Java
   2. name = Sam, age = 37, language = JavaScript
   ```
   
   If there is no output or you get an error, verify the parameters included in the connection string.
