This directory contains examples demonstrating how to use the gaussdb-rust library with both synchronous and asynchronous APIs.
Before running the examples, make sure you have:
-
GaussDB or OpenGauss database running
# Using Docker (recommended) docker run --name gaussdb-test \ -e GS_PASSWORD=Gaussdb@123 \ -e GS_USERNAME=gaussdb \ -e GS_DATABASE=postgres \ -p 5433:5432 \ -d opengauss/opengauss:latest -
Rust environment
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
| Example | Description | Features |
|---|---|---|
sync_basic.rs |
Basic synchronous operations | Connection, queries, transactions |
sync_authentication.rs |
GaussDB authentication methods | SHA256, MD5_SHA256 auth |
sync_types.rs |
Data type conversions | All supported PostgreSQL types |
sync_transactions.rs |
Transaction management | Commit, rollback, savepoints |
sync_copy.rs |
COPY operations | Bulk data import/export |
| Example | Description | Features |
|---|---|---|
async_basic.rs |
Basic asynchronous operations | Connection, queries, transactions |
async_authentication.rs |
GaussDB authentication methods | SHA256, MD5_SHA256 auth |
async_types.rs |
Data type conversions | All supported PostgreSQL types |
async_transactions.rs |
Transaction management | Commit, rollback, savepoints |
async_copy.rs |
COPY operations | Bulk data import/export |
async_connection_pool.rs |
Connection pooling | High-performance connection management |
| Example | Description | Features |
|---|---|---|
tls_connection.rs |
TLS/SSL connections | Secure connections |
custom_types.rs |
Custom type definitions | Enums, composites, domains |
migration_example.rs |
Migration from rust-postgres | Step-by-step migration guide |
# Run a specific example
cargo run --example sync_basic
# Run with features
cargo run --example async_basic --features "with-chrono-0_4"
# Run with environment variables
DATABASE_URL="host=localhost user=gaussdb password=Gaussdb@123 dbname=postgres port=5433" \
cargo run --example async_basic# Run all synchronous examples
for example in examples/sync_*.rs; do
echo "Running $(basename $example)"
cargo run --example $(basename $example .rs)
done
# Run all asynchronous examples
for example in examples/async_*.rs; do
echo "Running $(basename $example)"
cargo run --example $(basename $example .rs)
doneSet these environment variables to customize database connection:
export DATABASE_URL="host=localhost user=gaussdb password=Gaussdb@123 dbname=postgres port=5433"
export GAUSSDB_HOST="localhost"
export GAUSSDB_PORT="5433"
export GAUSSDB_USER="gaussdb"
export GAUSSDB_PASSWORD="Gaussdb@123"
export GAUSSDB_DATABASE="postgres"host=localhost user=gaussdb password=Gaussdb@123 dbname=postgres port=5433 sslmode=prefer
Problem: Connection refused (os error 10061)
Solution:
- Ensure GaussDB/OpenGauss is running
- Check port number (5433 for GaussDB, 5432 for PostgreSQL)
- Verify firewall settings
Problem: password authentication failed
Solution:
- Check username and password
- Verify authentication method in pg_hba.conf
- Ensure user has proper permissions
Problem: database "test" does not exist
Solution:
- Use existing database (usually "postgres")
- Create database first:
CREATE DATABASE test;
Problem: SSL connection errors
Solution:
- Use
sslmode=disablefor testing - Install proper certificates for production
- Use
gaussdb-native-tlsorgaussdb-opensslcrates
- Use Connection Pooling: For high-concurrency applications
- Prepared Statements: For repeated queries
- Batch Operations: Use COPY for bulk data
- Async for I/O: Use tokio-gaussdb for I/O-bound applications
- Sync for CPU: Use gaussdb for CPU-bound applications
- Use TLS: Always enable TLS in production
- Strong Passwords: Use complex passwords
- Least Privilege: Grant minimal required permissions
- Parameter Binding: Always use parameterized queries
- Connection Limits: Set appropriate connection limits
To add new examples:
- Create a new
.rsfile in theexamples/directory - Follow the naming convention:
sync_*orasync_* - Include comprehensive error handling
- Add documentation comments
- Update this README.md