4

I use PostgreSQL 9.6.17 (installed on Linux server on LAN) and have Win10 (or Win7) clients with installed Python 3.7 or 3.8 and PyQt 5.13.2, and my PyQt5 app that uses that PostgreSQL server works ok, but I really don't know what I'm doing.

I'll describe the problem. If I don't have PostgreSQL installed on Win client (I don't need it because I use PostgreSQL on Linux server) or have installed it but not in the path, my app crashes with the error:

"QSqlDatabase: QPSQL driver not loaded".

So, I need some drivers. And I install PostgreSQL 9.6.x on Win client and have it in the path and after that everything works ok, but only if I had luck and picked the right PostgreSQL version.

The alternative is that I don't install PostgreSQL 9.6.x (why would I need to install it) on a client machine and just copy some dll files at the app directory. If I find the right files (drivers), everything works ok, but if I don't, the app crashes with the error:

"QSqlDatabase: QPSQL driver not loaded"

or worse, recognize the PostgreSQL database on the server, works ok sometimes, but after the first, second or third query it crashes.

Driver combination I copy to my app directory that works with PyQt 5.13.2:

Timestamp                      Size File
25/02/2016  07:19         1,015,973 libiconv-2.dll
07/01/2016  09:02         1,550,023 libintl-8.dll
10/07/2019  20:20           145,408 libpq.dll

If I install PyQt 5.14 or 5.15 (instead of PyQt 5.13.2), the app crashes, and I am afraid to upgrade PyQt5 to versions 5.14 and more.

So, my question is: How do I know which drivers (dll files) I should copy to my app directory with various versions of PyQt5, that's the mystery to me. For example, what drivers should I use with PyQt versions 5.14 or 5.15 because those 3 dlls from above don't work anymore? Finding the right drivers is completely random guess job and I really don't know what I'm doing and I can say I had luck with drivers for PyQt 5.13.2. And also, I don't know if copying the drivers to my app directory (or having them in some folder that is in the path) is the right way at all?

There's a fine answer here:

QT5: Failed to load psql driver in windows

"It's quite likely to be failing because there's a dependency of qsqlpsql.dll missing - either libpq.dll, or one of the libraries that libpq build requires." ... "You should use the same libpq your Qt's qsqlpgsql.dll was compiled against if possible."

If that's true, how should I know with what libpq library qsqlpgsql.dll was compiled in? I suppose PyQt 5.13.2's qsqlpgsql.dll is compiled with a different version than PyQt 5.14.x's qsqlpgsql.dll or even 5.15.x's qsqlpgsql.dll.

Also, this link has some clues: QPSQL driver not loaded Qt

And generally, what's the best way to work with PostgreSQL in PyQt5. It obviously doesn't work out of the box if (the right version of) PostgreSQL is not installed on a client machine or appropriate drivers are not in path.

UPDATE:
I will put the question through hypothetical example. Let's suppose I have an app in Qt (PyQt), version Qt x.y.z (for example 5.15.0) on user client Win10 computers and Postgres a.b.c (for example 9.6.17) installed on Linux server that is reached from client computers through LAN (or VPN, or whatever). I'm not supposed to install Postgres on client Win10 computers. What files (Postgres dll or lib drivers) depending on Qt x.y.z and Postgres a.b.c do I have to have in user's app directory or path in order my app works correctly, and how to find that files. So, I need a formula: (*.dll, *.dll,,,) = f(x.y.z, a.b.c).

UPDATE (2020-09-25):
My problem is solved all by itself. With the combination Py 3.8.3, PyQt 5.15.1 it now works ok. I tested it on several Win client computers.

nenad
  • 164
  • 6
  • 17

2 Answers2

1

So in your update sections You have 2 systems:

System 1) Server with Linux OS e.g.

  • maybe ( Qt version 5.15.0 )
  • PostgreSql 9.6.17
  • maybe ( Python 3.1.1 )

System 2) Client with Win OS

  • Qt version 5.1.0
  • PostgreSql 9.6.17
  • Python 3.1.1

I would suggest your client system should have at least minimum you server library (here 9.6.17) versions. Or check library compatibility for each library separate.

suggestion A: why not building PyQt from source with libversion of your choise for system 2 and maybe for system 1 (if needed) and you will have your *.dll and *.so files for both systems. Please read the article how to configure qt with specific driver support: how to add db driver xy to qt

suggestion B Pack your python app via python installer pyinstaller inlcuding your dll files.

general win-os workflow of shipping your app

A) to use all dll files (for qt and PostgreSQL) from your build environment and copy them in the same folder like the executable. Maybe you use the program Dependency Walker
to figure out, which of these are linked. (most of the times found in qt installation directory)

B) Ship your app with all dll files, and make sure the destination os has the same architecture ( i86| amd64| etc) and also the same PostgreSQL server version is used.

t2solve
  • 657
  • 4
  • 20
  • Thanks for your answer. The thing is, choosing python and PyQt I thought I would avoid working with C++, that's the whole point of choosing PyQt. I even don't have Qt installed, only PyQt, so I don't have dlls (libpq.dll,...) in Qt build environment because I don't have it. I just need a way to somehow get appropriate dll files for Postgres for PyQt5.14 and PyQt5.15 because dll files that work with PyQt5.13 don't work anymore with higher PyQt versions. – nenad Sep 09 '20 at 18:20
  • mmh i got the point, but in general pyQt in just binding Cpp Qt-Framework code, so in the far end there will be somewhere an dll or some static library which is call via binding code in python, or do I misundertand something ? – t2solve Sep 10 '20 at 06:55
  • You are right, probably there are libraries "somewhere", but I can't find them. I need libraries for Postgres 9.6 for PyQt 5.15. And also I would like to know what is the procedure in the future, how to find appropriate libraries for certain combination of version of Postgres and version of PyQt/Qt. – nenad Sep 10 '20 at 08:18
  • maybe I do think to much in a "cpp way" of shipping your app, maybe you should use pip to install the deps via https://www.riverbankcomputing.com/software/pyqt/download5 ? – t2solve Sep 10 '20 at 08:37
  • The problem is that drivers for PSQL are not integral part of PyQt, so after installing PyQt with pip connecting to Postgres doesn't work out of the box (unlike Sqlite for example). – nenad Sep 10 '20 at 14:43
  • this sound really like you might should build PyQt with your specific driver, next producing your own python package and ship your app with your own package like: https://medium.com/@Lola_Dam/building-pyqt5-for-python-2-on-linux-b054f697b60c – t2solve Sep 11 '20 at 12:51
  • I don't get it, my problem is that I don't have "my specific driver" for Postgres 9.6 and PyQt 5.15. – nenad Sep 11 '20 at 14:04
  • "@t2solve" Server is PostgreSql 9.6.17 (no Py, No Qt on server which are irrelevant for server), Client is Win10, Py 3.8.3, PyQt 5.13.2 and it works perfectly. If I pip install PyQt5 -U, it installs PyQt 5.15.1 (I think) and all of a sudden I can't connect to database (all other things remaining the same). So, I downgrade to PyQt 5.13.2 and it again works perfectly, but now I am stuck with PyQt 5.13.2. for good :( That's my problem. – nenad Sep 24 '20 at 18:17
0

I realize that your target is just to establish a connection with your PostgreSQL server and interact with your DB.

So you can write bare python service to interact with a PostgreSQL server. With the use of psycopg2-binary, I think, you can easily achieve what you want.

If you implement it this way, there is no need in PostgreSQL drivers on your client machine.

Alex
  • 1,457
  • 1
  • 13
  • 26
trushad0w
  • 46
  • 3
  • 1
    I understand that (and used psycopg2 a lot), but that way I should abandon all the blessings of Qt MVC (QtSql classes like QSqlQueryModel, QSqlQuery,...) and start to write my app (SQL part) from scratch, which is unacceptable. – nenad Sep 10 '20 at 14:51
  • It's just the matter of what dlls uses someone who has Qt 5.15.x or PyQt 5.15.x app and works with remote Postgres 9.6.x server (just the matter of few right dll files). It's complete mystery to me how one should know what dll files go with e.g. Qt 5.15 and Postgres 9.6, what dll files go with Qt 5.13 and Postgres 10, what dll files go with Qt 5.12 and Postgres 11,... – nenad Sep 10 '20 at 14:58