当我的密码包含 at 符号 (@) 时,如何指定 Diesel DATABASE_URL?

Muh*_*fil 6 rust rust-diesel

我按照 Diesel 的官方文档开始使用。我还安装了 PostgreSQL。我的数据库用户名是postgres,密码是1schoollife@

我开始于

$ echo DATABASE_URL=postgres://postgres:1schoollife@@localhost/diesel_demo > .env
$ diesel setup
Run Code Online (Sandbox Code Playgroud)

结果:

Creating migrations directory at: /home/naufil/Desktop/rust/3june/testing/migrations
Creating database: diesel_demo
database "diesel_demo" already exists
Run Code Online (Sandbox Code Playgroud)

我创建了一个迁移:

$ 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
Run Code Online (Sandbox Code Playgroud)

迁移数据库时出现以下错误:

$ 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.
Run Code Online (Sandbox Code Playgroud)

She*_*ter 6

DATABASE_URL提供的不是您尝试使用的 URL。@是将凭据与主机名分隔开的特殊字符。由于您的凭据包含@,因此需要对其进行 URL 转义:

postgres://postgres:1schoollife%40@localhost/diesel_demo
Run Code Online (Sandbox Code Playgroud)

柴油维护员sgrif 说

是的,百分比编码对我们来说是 100% 正确的事情。

这已在PR 1058中实施