Search code examples
pythonpostgresqlpostgisqgisdatabase-table

Get table names from POSTGIS database with PyQGIS


How can I access the table names inside a POSTGIS database with PyQGIS? I am trying to load a layer from A POSTGIS database. I can do it if I know the table's name which I am gonna use.


Solution

  • If you want list of tables name from current database.

    from PyQt4.QtSql import *
        db = QSqlDatabase.addDatabase("QPSQL");
        db.setHostName("localhost");
        db.setDatabaseName("postgres");
        db.setUserName("postgres");
        db.setPassword("postgres");
        db.open();
        names=db.tables( QSql.Tables)
        print names