liveBook · Manning

Database connectivity

As explained in Rust Servers, Services, and Apps

Database connectivity is the ability of an application to establish communication with and exchange operations with a database.

Database connectivity lets a Rust application communicate with PostgreSQL, establish an asynchronous connection pool, execute a query, and display returned rows. A practical sanity test begins outside the web server, isolating setup and configuration problems before integration. section 4.2.2

A working connectivity setup proceeds through these steps:

  • Add SQLx with PostgreSQL, Tokio runtime, macros, and Chrono features. section 4.2.2
  • Install PostgreSQL, create the ezytutors database and user, and grant access privileges. section 4.2.2
  • Define a Rust course model, create and seed its table, load DATABASE_URL, connect with PgPool, and query a row. section 4.2.2

Connectivity sanity test

Set DATABASE_URL to a PostgreSQL connection string, run the vanilla Rust binary with cargo run, and verify terminal output containing the seeded First course row. section 4.2.2

[dependencies]
#Actix web framework and run-time                                  #1
actix-web = "4.1.0"
actix-rt = "2.7.0"
#Environment variable access libraries
dotenv = "0.15.0"                                                  #2
 
#Postgres access library
sqlx = {version = "0.6.2", default_features = false, features =
["postgres","runtime-tokio-native-tls", "macros","chrono"]}      #3
 
# Data serialization library
serde = { version = "1.0.144", features = ["derive"] }             #4
 
# Other utils
chrono = {version = "0.4.22", features = ["serde"]}                #5
 
# Openssl for build (if openssl is not already installed on the dev server)
openssl = { version = "0.10.41", features = ["vendored"] }         #6

Inspect the Cargo dependencies that configure Actix, dotenv, SQLx, PostgreSQL, Tokio, serialization, and date-time support. section 4.2.2

How Rust Servers, Services, and Apps covers it

How deep it goes
Builds connectivity from dependency configuration through credentials, schema, environment variables, pooling, querying, and terminal verification.
Examples
Uses the ezytutors database, truuser credentials, ezy_course_c4 table, seeded courses, and a course-selection query.
What it assumes
Assumes Rust project familiarity while teaching PostgreSQL installation, credentials, schema creation, SQLx configuration, and connection setup.
What you can do after
Diagnose database setup and query PostgreSQL with SQLx, then inspect returned course records in the terminal.
How it connects
Connects asynchronous programming, connection pools, PostgreSQL, SQLx, environment variables, database schemas, and querying.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.