Connecting to PostgreSQL with Diesel
Now that our database is running, in this section, we are going to build a connection to this database. We do this by performing the following steps:
- First, we utilize the
dieselcrate. In order to do this, we can add the following dependencies to ourcargo.tomlfile:diesel = { version = "1.4.4", features = ["postgres"]. } dotenv = "0.15.0"In the preceding code, we have included a
postgresfeature in ourdieselcrate. We have also included thedotenvcrate. This crate enables us to define variables in a.envfile, which will then be passed through into our program. We will use this to pass it in the database credentials and then into processes. - Now that we have this defined, we also need to install the
dieselclient. This is because we will be running migrations to the database through our terminal as opposed to our app. We can do this with the following command:cargo install diesel_cli --no-default-features...