I’m following along zero to production book and I’ve reached the point where I’m supposed to create a database connection with sqlx and Docker. I’ve been following all the steps in order to establish a connection to a DB and migrate to create a users table but I keep receiving this error message: “error with configuration: no driver found for URL scheme “postgres””
I am unexperienced with DB so I’m not even sure to understand what the error message means.
This is what my bash looks like:
#!/usr/bin/env bash
set -x
set -eo pipefail
if ! [ -x "$(command -v psql)" ]; then
echo >&2 "Error: psql is not installed."
exit 1
fi
if ! [ -x "$(command -v sqlx)" ]; then
echo >&2 "Error: sqlx is not installed."
echo >&2 "Use:"
echo >&2 " cargo install --version='~0.7' sqlx-cli --no-default-features --features rustls,postgres"
echo >&2 "to install it."
exit 1
fi
# Check if a custom user has been set, otherwise default to 'postgres'
DB_USER="${POSTGRES_USER:=postgres}"
# Check if a custom password has been set, otherwise default to 'password'
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
# Check if a custom database name has been set, otherwise default to 'lairbnb'
DB_NAME="${POSTGRES_DB:=lairbnb}"
# Check if a custom port has been set, otherwise default to '5432'
DB_PORT="${POSTGRES_PORT:=5432}"
# Check if a custom host has been set, otherwise default to 'localhost'
DB_HOST="${POSTGRES_HOST:=localhost}"
# Allow to skip Docker if a dockerized Postgres database is already running
if [[ -z "${SKIP_DOCKER}" ]]
then
# if a postgres container is running, print instructions to kill it and exit
RUNNING_POSTGRES_CONTAINER=$(docker ps --filter 'name=postgres' --format '{{.ID}}')
if [[ -n $RUNNING_POSTGRES_CONTAINER ]]; then
echo >&2 "there is a postgres container already running, kill it with"
echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}"
exit 1
fi
# Launch postgres using Docker
docker run
-e POSTGRES_USER=${DB_USER}
-e POSTGRES_PASSWORD=${DB_PASSWORD}
-e POSTGRES_DB=${DB_NAME}
-p "${DB_PORT}":5432
-d
--name "postgres_$(date '+%s')"
postgres -N 1000
# ^ Increased maximum number of connections for testing purposes
fi
# Keep pinging Postgres until it's ready to accept commands
until PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c 'q'; do
>&2 echo "Postgres is still unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!"
export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
sqlx database create
sqlx migrate run
>&2 echo "Postgres has been migrated, ready to go!"
It’s virtually the same than the one in the book, so I’m confident it’s not a typo error
This is what my cargo tool looks like:
[lib]
path = "src/lib.rs"
[[bin]]
path = "src/main.rs"
name = "lairbnb_rs"
[dependencies]
actix-web = "4"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
serde = "1.0.115"
config = { version = "0.13", default-features = false, features = ["yaml"] }
sqlx = { version = "0.7", default-features = false, features = ["runtime-tokio-rustls", "macros", "postgres", "uuid", "chrono", "migrate"] }
uuid = { version = "1", features = ["v4"] }
chrono = { version = "0.4.22", default-features = false, features = ["clock"] }
[dev-dependencies]
reqwest = { version = "0.11", features = ["json"] }
And finally, this is what my migration file looks like:
-- Create Registration Table
CREATE TABLE users(
id uuid NOT NULL,
PRIMARY KEY (id),
account_name text UNIQUE NOT NULL PRIMARY KEY,
account_password text NOT NULL
);
Would anyone know what the error message could be referring to perhaps? And maybe how I could hope to fix it?
Cheers
I tried to create a connection to a DB using sqlx and Docker, however I only managed to get this error message :
+ set -eo pipefail
++ command -v psql
+ '[' -x /opt/homebrew/bin/psql ']'
++ command -v sqlx
+ '[' -x /Users/lunaferraraccio/.cargo/bin/sqlx ']'
+ DB_USER=postgres
+ DB_PASSWORD=password
+ DB_NAME=lairbnb
+ DB_PORT=5432
+ DB_HOST=localhost
+ [[ -z '' ]]
++ docker ps --filter name=postgres --format '{{.ID}}'
+ RUNNING_POSTGRES_CONTAINER=
+ [[ -n '' ]]
++ date +%s
+ docker run -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=password -e POSTGRES_DB=lairbnb -p 5432:5432 -d --name postgres_1721673957 postgres -N 1000
22c73dffb9d331b62cfff2271be596312e9f717c8215e4db990d49be702ecb55
+ PGPASSWORD=password
+ psql -h localhost -U postgres -p 5432 -d postgres -c 'q'
+ echo 'Postgres is up and running on port 5432 - running migrations now!'
Postgres is up and running on port 5432 - running migrations now!
+ export DATABASE_URL=postgres://postgres:password@localhost:5432/lairbnb
+ DATABASE_URL=postgres://postgres:password@localhost:5432/lairbnb
+ sqlx database create
error: error with configuration: no driver found for URL scheme "postgres"
I am unexperienced in databases and I would love to get a better understanding of the situation in order to improve myself.