1

I have following working database connection setup for my Rocket app:

main.rs:

#[database("my_db")]
pub struct DbConn(diesel::PgConnection);

Rocket.toml:

[global.databases]
my_db = { url = "postgres://user:pass@localhost/my_db" }

I would like to set username, password and a database name from the environment. Expected it to be something like ROCKET_MY_DB=postgres://user:pass@localhost/my_db, but it didn't work. Was unable find relevant database example for Rocket.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
SS_Rebelious
  • 1,274
  • 2
  • 19
  • 35

2 Answers2

10

After a lot of experiments (as there is no specific instructions for the database and I expected something that looked more like a standard approach: ENV_PARAM=conn_string, i.e. in Diesel) I finally figured out that I need to place a complex object into the environment.

The solution is this ugly string:

ROCKET_DATABASES={my_db={url="postgres://user:pass@localhost/my_db"}}

SS_Rebelious
  • 1,274
  • 2
  • 19
  • 35
0

I would like to set username, password and a database name from the environment. Didn't find relevant example for Rocket.

Front page of the doc

Rocket and Rocket libraries are configured via the Rocket.toml file and/or ROCKET_{PARAM} environment variables. For more information on how to configure Rocket, see the configuration section of the guide as well as the config module documentation.

Example just follow link:

All configuration parameters, including extras, can be overridden through environment variables. To override the configuration parameter {param}, use an environment variable named ROCKET_{PARAM}. For instance, to override the "port" configuration parameter, you can run your application with:

ROCKET_PORT=3721 ./your_application

  Configured for development.
    => ...
    => port: 3721 ```
Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • 4
    Thanks, I saw these instructions. My question was specific for the database setup as it is not clear from the docs which parameter is needed and how to construct it. In most of the frameworks (including Diesel) this requires single connection string argument. The answer I expected (figured out finally): `ROCKET_DATABASES={my_db={url="postgres://user:pass@localhost/my_db"}}` – SS_Rebelious Feb 02 '20 at 07:41