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
diesel
crate. In order to do this, we can add the following dependencies to ourcargo.toml
file:diesel = { version = "1.4.4", features = ["postgres"]. } dotenv = "0.15.0"
In the preceding code, we have included a
postgres
feature in ourdiesel
crate. We have also included thedotenv
crate. This crate enables us to define variables in a.env
file, 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
diesel
client. 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...