Diesel.rs —PostgreSQL linking errors on Windows
Recently I started a new web application that’ll use a Rust server. I used one of the most popular ORM for Rust: diesel.rs.
Unfortunately, I ran into some issues that seem to be relatively common (judging by the Google results I’ve seen). I decided to take a moment to write down my findings and document them here in this article.
It basically boils down to some problems related to environment variables on Windows. The documentation is pretty clear, but I found it to be a bit lacking when it came to getting PostgreSQL and diesel.rs working nicely on Windows.
This article will show how to resolve two related errors:
- LNK1181: cannot open input file “libpq.lib”
- Exit code: 0xc0000135, STATUS_DLL_NOT_FOUND
LNK1181: cannot open input file “libpq.lib”
When searching for information about this error, you’ll quickly find that you need to add your PostgreSQL lib
folder to your environment variables.
This is pretty easy to do, but it may not be immediately obvious for people who haven’t dealt with this kind of thing on Windows before.
- Open the Windows search bar:
Win+S
- Search for “environment variable”
- Click on the option called “edit the system environment variables”
- In the pop-up, click the “environment variables…” button
- Under the user environment variables section, add a new environment variable called
PQ_LIB_DIR
and give it a value ofC:/Program Files/PostgreSQL/13/lib
(this path may be different depending on where you’ve installed PostgreSQL on your device). - Click “ok” to save your changes.
- Restart your command-prompt / IDE instances.
The last step is very important as new environment variables will only be detected whenever an application starts. I use Visual Studio Code as my IDE, so simply restarting the application does the trick.
You could also simply reboot your machine. This is a bit drastic and unnecessary, but it’ll definitely do the trick!
Try to build your Rust application again. It should now build without any errors!
If you try to run the application and it crashes with error code 0xC0000135
, please keep reading as we’ll tackle that one next!
Exit code: 0xc0000135, STATUS_DLL_NOT_FOUND
This error code took me about an hour or so to solve. The solution is very simple, but since I wasn’t too familiar with PostgreSQL and diesel.rs, it wasn’t obvious to me.
As the name suggests, the application is unable to find a certain DLL file. Unfortunately, our console window does not show much more information than that.
To fix the problem, first open your environment variables again (check the previous chapter to learn how to do this).
The C:/Program Files/PostgreSQL/13/lib
directory needs to be added to your PATH
variable under the user’s environment variables section.
Open the PATH
environment variable by selecting it and clicking on the “edit” button. In the new window that appears, click “new” and paste the path to the lib
folder of the PostgreSQL installation.
Click on “ok” and save your changes. Be sure to restart your IDE to make sure that the new PATH
value will be picked up!
Try running your Rust application again. It should now launch successfully!
Closing words
That’s all for now, folks! It’s a really short article this time, but I hope that any of this has helped you to fix some common PostgreSQL and diesel.rs issues.
Cheers!