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
