Search code examples
rustrust-diesel

How do I specify a Diesel DATABASE_URL when my password contains an at-sign (@)?


I am getting started with Diesel by following their official documentation. I also have PostgreSQL installed. My database username is postgres and the password is 1schoollife@.

I started with

$ echo DATABASE_URL=postgres://postgres:1schoollife@@localhost/diesel_demo > .env
$ diesel setup

results:

Creating migrations directory at: /home/naufil/Desktop/rust/3june/testing/migrations
Creating database: diesel_demo
database "diesel_demo" already exists

I created a migration:

$ diesel migration generate create_posts
Creating migrations/2019-06-03-182531_create_posts/up.sql
Creating migrations/2019-06-03-182531_create_posts/down.sql

I get the following error when migrating the database:

$ diesel migration run
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ConnectionError(BadConnection("could not translate host name \"@localhost\" to address: Name or service not known\n"))', src/libcore/result.rs:997:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

Solution

  • The DATABASE_URL you have provided is not the URL you are attempting to use. @ is a special character that separates the credentials from the hostname. Since your credentials contain an @, it needs to be URL escaped:

    postgres://postgres:1schoollife%40@localhost/diesel_demo
    

    Diesel maintainer sgrif said:

    Yes, percent encoding is 100% the correct thing for us to be doing.

    This was implemented in PR 1058