In the previous 2 modules we looked at the basic concepts, features and functions of relational databases as well as extensions that let us store, manage, query and manipulate spatial data in a relational database. This section will dive deeper into how to effectively use spatial databases in QGIS.
The goal for this lesson: To learn how to interact with spatial databases using the QGIS Browser interface.
We have already briefly looked at how to add tables from a database as QGIS layers, now lets look at this in a bit more detail and look at the different ways this can be done in QGIS. Lets start by looking at the new Browser interface.
Informacja
Of course you can also use this interface to connect to PostGIS databases hosted on a server external to your workstation. Right clicking on the PostGIS entry in the tree will allow you to specify connection paramaters for a new connection.
Now that we have seen how to add an entire table as a QGIS layer it might be nice to learn how to add a filtered set of records from a table as a layer by using queries that we learned about in previous sections.
Start a new empty map with no layers
Click the Add PostGIS Layers button or select Layer –> Add PostGIS Layers from the menu.
In the Add PostGIS Table(s) dialog that comes up, connect to the postgis_demo connection.
Expand the public schema and you should find the three tables we were working with previously.
Click the lines layer to select it, but instead of adding it, click the Set Filter button to bring up the Query Builder dialog.
Construct the following expression using the buttons or by entering it directly:
"roadtype" = 'major'
You will notice that only the Primary Roads have been added to your map rather than the entire layer.
You have seen how to interact with spatial databases using the QGIS Browser and how to add layers to your map based on a query filter.
Next you’ll see how to work with the DB Manager interface in QGIS for a more complete set of database management tasks.