How to get Diesel/PostgreSQL to work on Windows

This is kind of a follow-up to my post yesterday, which turned out to be an issue with Diesel. I have now resolved it, and thought it worthwhile recording how I did it, as it was not straightforward, and I could not find any clear instructions aywhere on-line. This seems to be specifically an issue for Windows; I was using Windows 10.

I followed the instructions here. The first trick, which is mentioned there is to just include PostgreSQL, using this install command:

cargo install diesel_cli --no-default-features --features postgres

However, this gave me the link.exe error I posted about previously. It comes down to telling Diesel where to find PosteSQL.

I had to use chocolatey (a Windows alternative to "sudo apt install") to install PostgreSQL. This is in addition to the normal install. As my install of PostgreSQL is version 10, I used this command, which needs admin rights to run:

choco install postgresql10

I also had to set an environment variable so Diesel could find it:

set PQ_LIB_DIR="C:\ProgramData\chocolatey\lib\postgresql10"

So then I got to this command in the tutorial:

diesel setup

At this point I got a new error about a file not being found - libpq.dll. To get this to work I had to add two folders to the path. These are to the original full installation, not the chocolalatey install:

set path=%path%;C:\Program Files\PostgreSQL\10\lib;C:\Program Files\PostgreSQL\10\bin

With all that in place, I could complete the tutorial.

One slight issue in, I changed line 13 from:

let title = &title[..(title.len() - 1)];

let title = title.trim();

The line is stripping the new line character from the input. I suspect the original is faster, but only removes the last character. My version removes both \r and \n, necessary for Windows.

1 Like