Fixing the "cannot open input file 'libpq.lib'" error on windows

Rust diesel_cli build on windows using postgres

June 2021, written for diesel 1.4.2


If you're reading this, you're probably having issues installing diesel_cli on windows for postgres dbs. Thankfully the fix is very easy.


Requirements

  • Local Postgres installation (this guide assumes 13, but any is fine)
  • Rust

Use rustup show to find your active toolchain.



Toolchain: pc-windows-msvc

Error: fatal error LNK1181: cannot open input file 'libpq.lib'

Make sure you have this environment variable set, otherwise add it: PQ_LIB_DIR=C:\Program Files\PostgreSQL\13\lib

Restart the terminal, the build should now work.



Toolchain: pc-windows-gnu

Linker error, problem finding -lpq.

This error is a simple naming issue. Postgres installs libpq.lib during normal installation, but diesel expects pq.lib.


We want to copy libpq.lib with a new name -> pq.lib.

You can use the following command in bash or powershell (requires admin rights):

cp "C:\Program Files\PostgreSQL\13\lib\libpq.lib" "C:\Program Files\PostgreSQL\13\lib\pq.lib"


Then, same as the msvc toolchain, make sure you have this environment variable set, otherwise add it: PQ_LIB_DIR=C:\Program Files\PostgreSQL\13\lib

Restart the terminal, the build should now work.