How to read database connecion from .env in Rocket app

I need help to read database connection string from .env file

The rocket docs suggests the connection is set in Rocket.toml file. I found a post stackoverflow with same problem But the solution is implemented for Rocket v0.4x and I am using version 0.5-rc

Are you thinking of the the dotenv crate? It lets you set ROCKET_DATABASES and other environment variables in a file named .env.

Not really, my application is Dockerized, I want to be able to pass the Database connection string in when running the app rather than setting it in Rocket.toml

Tae a look at Figment. Its documentation is pretty clear on how to read a config value from an environment variable. Plus it's from the creator of Rocket, so I assume that Rocket is using it as its config library.

1 Like

You might consider using the config crate.

Luca Palmieri has a tutorial on setting up a database connection using the config crate in the book Zero to Production in Rust.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.