My system: Windows 10 Pro 16299, Qt, PyQt 5.11.2, Python 3.6, PostgreSql 10
I tried to use QTableView/QSqlTableModel for in my gui to work with postgresql data. However, I am not able to open the database. I get the error message “Driver not loaded Driver not loaded”.
A new installation of Qt, PostgreSql and PyQt has not solved the problem. I tried also “Dependency Walker” to look for missing dlls, but was not able use the given information.
Do you have an idea how to fix this problem?
As an alternative: Is it possible to use QTableView/QSqlTableModel with psycopg2 (instead of QSqlDatabase)?
Thank you very much in advance!
from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel
from PyQt5.QtWidgets import QTableView, QApplication
import sys
if __name__ == '__main__':
app = QApplication(sys.argv)
db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName("localhost")
db.setPort(5432)
db.setDatabaseName("Test")
db.setUserName("postgres")
db.setPassword("xxxxx")
if (db.open() == False):
QMessageBox.critical(None, "Error", db.lastError().text())
else:
Print("Connected")
Dependency Walker Screenshot
Find the Postgres driver:
C:\Python\Python36\Lib\site-packages\PyQt5\Qt\plugins\sqldrivers\qsqlpsql.dll
where C:\Python\Python36
is your Python installation directory.
Load this file with dependency walker and check whether libpq.dll
and all its dependencies can be found.
Typically the two Postgres folders have to be set on the system path:
C:\Program Files\PostgreSQL\10\bin\
C:\Program Files\PostgreSQL\10\lib\
The dll's version incompatibility (64-bit vs 32-bit) may be one of the probable problems.