I'm trying to load a layer from a PostGIS database using PyQGIS. The code is:

import os
from qgis.core import *

project = QgsProject.instance()

uri = QgsDataSourceUri()
uri.setConnection("localhost", "5432", "db_name", "user", "pass")
uri.setDataSource("public", "table_name", "the_geom", "")
vlayer = QgsVectorLayer(uri.uri(False), "table_name", "postgres")
if not vlayer.isValid():
    print("Layer failed to load!")

The problem is that I don't know what to write in "the_geom" (I tried to insert "point" or something similar).

Is there someone who can tell me?

  • We can't know what the geometry column in your table is unless you give us the table definition in the question body. While geom and shape are common, there really is nothing stopping the column from being named avacado_field or fhhfghjitrewqutrchhfvhhgghgd.
    – Vince
    Commented Mar 5, 2020 at 11:32

1 Answer 1


uri.setDataSource("public", "table_name", "the_geom", "query")

  • public refers to the name of your schema
  • table_name refers to the table you want to load
  • the_geom refers to the name of the geometry column (usually the_geom or geom)
  • query refers to a filter, if you don't put anything in, the whole table will be loaded. For example, id > 10
  • Doesn't "geom", need to be followed by "polygon" or "point" in this case?
    – BJW
    Commented Mar 5, 2020 at 10:33
  • Replace "geom" with the name of your geometry column. Commented Mar 5, 2020 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.