What is Xata?

Connect to Postgres with rust sqlx

Edit on GitHub

To use Xata with Rust sqlx, you have the option to connect with the connection string (DSN). Please replace the placeholders in the connection string, as indicated on the connect to Postgres page. For this example sqlx is used with tokio and rustls.

use sqlx::FromRow;
use sqlx::{Connection, PgConnection};

async fn main() -> Result<(), sqlx::Error> {
    let conn_string = "postgresql://<WORKSPACE_ID>:<API_KEY>@<REGION>.sql.xata.sh:5432/<DATABASE_NAME>:<BRANCH_NAME>?sslmode=require";

    let mut conn = PgConnection::connect(conn_string).await?;

    sqlx::query("CREATE TABLE IF NOT EXISTS test(id serial, name text);")
        .execute(&mut conn)

    let _row: (i32,) = sqlx::query_as("INSERT INTO test (name) VALUES($1) RETURNING id")
        .fetch_one(&mut conn)

    #[derive(Debug, FromRow)]
    struct TestRow {
        id: i32,
        name: String,

    let select_query = sqlx::query_as::<_, TestRow>("SELECT id, name FROM test");
    let rows: Vec<TestRow> = select_query.fetch_all(&mut conn).await?;
    println!("\n=== select test rows: \n{:?}", rows);

    sqlx::query("DROP TABLE IF EXISTS test;")
        .execute(&mut conn)



Cargo dependencies

tokio = { version = "1", features = ["full"] }
sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "postgres" ] }