2

This question already has an answer here:

I have a layer that is defined by a complex SQL query. It works fine but I would like to move that complexity out of QGIS and into the data base so I can access it from elsewhere (GeoServer).

I have tried creating a postgres view which represents the layer by using the database manager in QGIS (save as view) and by creating the view from the database itself but when I try and load the layer it is marked with a '!' and I can not select it.

Yes this is a duplicate question (sorry I don't know how to insert a reference to it) but the answers to that question don't work with current (2.18) versions of QGIS. In particular they do not include the step to click on the schema button which is what I was missing. I suspect that needing to do this is a bug but I will check this in QGIS 3 before reporting it as such.

The process of adding views is rather unintuitive and 'Get Spatial's explanation is clearer than any of the answers to the original question. If we dump my question (feel free :) then please ensure that this answer gets added to the original one.

I note that several people besides me have up voted that answer.

marked as duplicate by JGH, whyzar, aldo_tapia, HDunn, BERA Nov 2 '17 at 13:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

You need to define the Feature id field for the layer in question.

Look/scroll to the right until you see the column for Feature id. This needs to be defined before QGIS will recognize it as a valid layer. Note that it says "Select" in that section for each view. QGIS - Add PostGIS Layer

Click the word "Select". This will open a pulldown and also seem to populate what QGIS guesses is the Unique ID. You MUST actually check the box next to the appropriate field for it to register. QGIS - Select Unique ID - PostGIS

Once you have done this, click on the Schema name at the far left. This will cause the exclamation point to go away. Click again to select the layer, then click Add. QGIS - PostGIS Select Layer

It should load in without any issues at this point. QGIS - PostGIS Layer Loaded

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