1

I'm trying to use the SQLTableModel from PyQt6 (installed with pip) in Python 3.11. For this I need to connect to my Postgresql 13 database (hosted on AWS) using the QPSQL driver from PyQt6.

The problem is in this step, when I try to execute the open from database it fails indicating that:

QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QPSQL
QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins

The code I use for the test is the following:

db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName('host')
db.setDatabaseName("dbname")
db.setUserName('user')
db.setPassword('pass')
db.setConnectOptions("-c debug_print_parse=on -c debug_print_rewritten=on -c debug_print_plan=on -c debug_pretty_print=on")

if not db.open():
    print("Error al abrir la base de datos:",db.lastError().text())

You might think that the problem lies in the required QCoreApplication instance; however, by adding

app = QCoreApplication(sys.argv)

the same message results without the last line, that is

QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QPSQL 

I have tried a solution that mentions adding environment variables to Postgresql's Lib and Bin folder path, solution mentioned in this link https://www.pythonguis.com/faq/postgres-pyqt5-windows-driver-not-loaded/ in both user and system variables twice unsuccessfully.

I read in the Qt documentation a section that says "How to Build the QPSQL Plugin on Windows" in this link https://doc.qt.io/qt-5/sql-driver.html which indicates the following: Install the appropriate PostgreSQL developer libraries for your compiler. Assuming that PostgreSQL was installed in C:\psql, build the plugin as follows:

cd C:\Users\Raknaros\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\PyQt6\Qt6\plugins\sqldrivers
qmake -- PSQL_INCDIR=C:\Program Files\PostgreSQL\13\include
 PSQL_LIBDIR=C:\Program Files\PostgreSQL\13\lib
nmake sub psqlcmake
nmake install

Which I can't do either since the qmake command is not recognized in my Windows CMD.

I also tried to copy the libpq.dll and libpq.lib inside the folder

C:\Users\Raknaros\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\PyQt6\ Qt6\plugins\sqldrivers and inside the folder where my .py file is

And the driver still doesn't work.

I am testing Dependency Walker, I leave here the image Dependency Walker that appears when scanning the qsqlpsql.dll file, as far as I can see there are many dependencies not found.

I have no idea what I can do to make the driver work and finish my desktop application with PyQt6, this situation is desperate, I hope you can help me load the QPSQL driver please.

I would like to apply the compilation described in this link https://doc.qt.io/qt-6.2/sql-driver.html, but I don't know how, could you please help me with an example or an exact description.

My settings is: Windows 10 64 Python 3.11 PyQt6

2 Answers 2

0

I also tried to copy the libpq.dll and libpq.lib inside the folder

That should not work unless the dependencies of libpq.dll are also copied. In an PostgreSQL installation, you'll find a bunch of DLL files in the bin/ directory where the client-side binaries (psql.exe, pg_dump.exe, etc...) are located. Copy all these *.dll files along with libpq.dll into a folder from where your app can find them.

On the other hand, if the problem is that the dependencies of libpq.dll cannot be found and you have tried adding the Postgres bin directory to the system %PATH%, that method should have worked in the first place.

0

What worked for me was to install the PostgreSQL ODBC driver for Windows. Version 10 works and I'm unclear if the newer versions work with PyQt.

You'll also have to add the correct bin folder to your system path. For me it was in

C:\Program Files\psqlODBC\1000\bin

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .