Skip to main content
edited tags
Link
Germán Carrillo
  • 37k
  • 5
  • 125
  • 180
added 42 characters in body; edited tags; edited title
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

PyQGIS: load Invalid PostgreSQL layer (always invalid)after loading it with PyQGIS

In a standalone PyQGIS script, I try to load a PostgreSQL (PostGIS) layer with the following code (see below). The

The layer is always invalid, however when entering the code directly into the Python shell within QGIS, the layer gets loaded without any problems and is valid.

I have already checked if the credentials are valid.

I'm currently using QGIS 3.2 Desktop on Ubuntu Desktop.

#!/usr/bin/python
 # -*- coding: UTF-8 -*-

from qgis.core import *

QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], True)
QgsApplication.initQgis()

uri = QgsDataSourceUri()
uri.setConnection('host', '5432', 'username', 'database_name', 'password')
uri.setDataSource ('public', 'table_name', 'geometry_column_name')
uri.setKeyColumn('primary_key_column_name')
vlayer=QgsVectorLayer (uri.uri(), 'layer_name', 'postgres')
print(uri.uri())
if not vlayer.isValid():
    print("!Layer could not be loaded")
else:
    QgsProject.instance().addMapLayer(vlayer)
#!/usr/bin/python
 # -*- coding: UTF-8 -*-

from qgis.core import *

QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], True)
QgsApplication.initQgis()

uri = QgsDataSourceUri()
uri.setConnection('host', '5432', 'username', 'database_name', 'password')
uri.setDataSource ('public', 'table_name', 'geometry_column_name')
uri.setKeyColumn('primary_key_column_name')
vlayer = QgsVectorLayer (uri.uri(), 'layer_name', 'postgres')
print(uri.uri())

if not vlayer.isValid():
    print("!Layer could not be loaded")
else:
    QgsProject.instance().addMapLayer(vlayer)

PyQGIS: load PostgreSQL layer (always invalid)

In a standalone PyQGIS script, I try to load a PostgreSQL (PostGIS) layer with the following code. The layer is always invalid, however when entering the code directly into the Python shell within QGIS, the layer gets loaded without any problems and is valid.

I have already checked if the credentials are valid.

I'm currently using QGIS 3.2 Desktop on Ubuntu Desktop.

#!/usr/bin/python
 # -*- coding: UTF-8 -*-

from qgis.core import *

QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], True)
QgsApplication.initQgis()

uri = QgsDataSourceUri()
uri.setConnection('host', '5432', 'username', 'database_name', 'password')
uri.setDataSource ('public', 'table_name', 'geometry_column_name')
uri.setKeyColumn('primary_key_column_name')
vlayer=QgsVectorLayer (uri.uri(), 'layer_name', 'postgres')
print(uri.uri())
if not vlayer.isValid():
    print("!Layer could not be loaded")
else:
    QgsProject.instance().addMapLayer(vlayer)

Invalid PostgreSQL layer after loading it with PyQGIS

In a standalone PyQGIS script, I try to load a PostgreSQL (PostGIS) layer with the following code (see below).

The layer is always invalid, however when entering the code directly into the Python shell within QGIS, the layer gets loaded without any problems and is valid.

I have already checked if the credentials are valid.

I'm currently using QGIS 3.2 Desktop on Ubuntu Desktop.

#!/usr/bin/python
 # -*- coding: UTF-8 -*-

from qgis.core import *

QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], True)
QgsApplication.initQgis()

uri = QgsDataSourceUri()
uri.setConnection('host', '5432', 'username', 'database_name', 'password')
uri.setDataSource ('public', 'table_name', 'geometry_column_name')
uri.setKeyColumn('primary_key_column_name')
vlayer = QgsVectorLayer (uri.uri(), 'layer_name', 'postgres')
print(uri.uri())

if not vlayer.isValid():
    print("!Layer could not be loaded")
else:
    QgsProject.instance().addMapLayer(vlayer)
Post Closed as "Duplicate" by Germán Carrillo pyqgis
title case; naming; removed appreciation
Source Link
Vince
  • 20.3k
  • 16
  • 48
  • 65

PyQGIS: Load Postgres Layerload PostgreSQL layer (always invalid)

In a standalone PyQGIS script, I try to load a PostgresPostgreSQL (postgisPostGIS) layer with the following code. The layer is always invalid, however when entering the code directly into the pythonPython shell within qgisQGIS, the layer gets loaded without any problems and is valid.

I have already checked if the credentials are valid.

I'm currently using QGIS 3.2 Desktop on Ubuntu Desktop. Any help would be highly appreciated.

#!/usr/bin/python
 # -*- coding: UTF-8 -*-

from qgis.core import *

QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], True)
QgsApplication.initQgis()

uri = QgsDataSourceUri()
uri.setConnection('host', '5432', 'username', 'database_name', 'password')
uri.setDataSource ('public', 'table_name', 'geometry_column_name')
uri.setKeyColumn('primary_key_column_name')
vlayer=QgsVectorLayer (uri.uri(), 'layer_name', 'postgres')
print(uri.uri())
if not vlayer.isValid():
    print("!Layer could not be loaded")
else:
    QgsProject.instance().addMapLayer(vlayer)

PyQGIS: Load Postgres Layer (always invalid)

In a standalone PyQGIS script, I try to load a Postgres (postgis) layer with the following code. The layer is always invalid, however when entering the code directly into the python shell within qgis, the layer gets loaded without any problems and is valid.

I have already checked if the credentials are valid.

I'm currently using QGIS 3.2 Desktop on Ubuntu Desktop. Any help would be highly appreciated.

#!/usr/bin/python
 # -*- coding: UTF-8 -*-

from qgis.core import *

QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], True)
QgsApplication.initQgis()

uri = QgsDataSourceUri()
uri.setConnection('host', '5432', 'username', 'database_name', 'password')
uri.setDataSource ('public', 'table_name', 'geometry_column_name')
uri.setKeyColumn('primary_key_column_name')
vlayer=QgsVectorLayer (uri.uri(), 'layer_name', 'postgres')
print(uri.uri())
if not vlayer.isValid():
    print("!Layer could not be loaded")
else:
    QgsProject.instance().addMapLayer(vlayer)

PyQGIS: load PostgreSQL layer (always invalid)

In a standalone PyQGIS script, I try to load a PostgreSQL (PostGIS) layer with the following code. The layer is always invalid, however when entering the code directly into the Python shell within QGIS, the layer gets loaded without any problems and is valid.

I have already checked if the credentials are valid.

I'm currently using QGIS 3.2 Desktop on Ubuntu Desktop.

#!/usr/bin/python
 # -*- coding: UTF-8 -*-

from qgis.core import *

QgsApplication.setPrefixPath("/usr/bin/qgis", True)
qgs = QgsApplication([], True)
QgsApplication.initQgis()

uri = QgsDataSourceUri()
uri.setConnection('host', '5432', 'username', 'database_name', 'password')
uri.setDataSource ('public', 'table_name', 'geometry_column_name')
uri.setKeyColumn('primary_key_column_name')
vlayer=QgsVectorLayer (uri.uri(), 'layer_name', 'postgres')
print(uri.uri())
if not vlayer.isValid():
    print("!Layer could not be loaded")
else:
    QgsProject.instance().addMapLayer(vlayer)
Source Link
nbfr
  • 41
  • 3
Loading