Eighteen: 18.1 Lesson: Working With Databases in The QGIS Browser
Eighteen: 18.1 Lesson: Working With Databases in The QGIS Browser
EIGHTEEN
In this module you will learn about how to use Spatial Databases with QGIS to manage, display and manipulate data
in the database as well as performing analysis by querying. We will primarily use PostgreSQL and PostGIS (which
were covered in previous sections), but the same concepts are applicable to other spatial database implementations
including SpatiaLite.
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.
• Start a new empty map in QGIS.
• Open the Browser by clicking the Browser tab at the bottom of the Layer Panel
• Open the PostGIS portion of the tree and you should find your previously configured connection available (you
may need to click the Refresh button at the top of the browser window).
609
QGIS Training Manual
• Double clicking on any of the table/layers listed here will add it to the Map Canvas.
• Right Clicking on a table/layer in this view will give you a few options. Click on the Properties item to look at
the properties of the layer.
Note: 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 parameters 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'
• Click OK to complete editing the filter and click Add to add the filtered layer to your map.
• Rename the lines layer in the tree to roads_primary.
You will notice that only the Primary Roads have been added to your map rather than the entire layer.
18.1.3 In Conclusion
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.
We have already seen how to perform many database operations with QGIS as well as with other tools, but now it’s
time to look at the DB Manager tool which provides much of this same functionality as well as more management
oriented tools.
The goal for this lesson: To learn how to interact with spatial databases using the QGIS DB Manager.
You should first open the DB Manager interface by selecting Database –> DB Manager –> DB Manager on the menu
or by selecting the DB Manager icon on the toolbar.
You should already see the previous connections we have configured and be able to expand the myPG section and its
public schema to see the tables we have worked with in previous sections.
The first thing you may notice is that you can now see some metadata about the Schemas contained in your database.
Schemas are a way of grouping data tables and other objects in a PostgreSQL database and a container for permissions
and other constraints. Managing PostgreSQL schemas is beyond the scope of this manual, but you can find more
information about them in the PostgreSQL documentation on Schemas. You can use the DB Manager to create new
Schemas, but will need to use a tool like pgAdmin III or the command line interface to manage them effectively.
DB Manager can also be used to manage the tables within your database. We have already looked at various ways to
create and manage tables on the command line, but now lets look at how to do this in DB Manager.
18.2. Lesson: Using DB Manager to work with Spatial Databases in QGIS 613
QGIS Training Manual
First, its useful to just look at a table’s metadata by clicking on its name in tree and looking in the Info tab.
In this panel you can see the General Info about the table as well the information that the PostGIS extension maintains
about the geometry and spatial reference system.
If you scroll down in the Info tab, you can see more information about the Fields, Constraints and Indexes for the table
you are viewing.
Its also very useful to use DB Manager to simply look at the records in the database in much the same way you might
do this by viewing the attribute table of a layer in the Layer Tree. You can browse the data by selecting the Table tab.
18.2. Lesson: Using DB Manager to work with Spatial Databases in QGIS 615
QGIS Training Manual
There is also a Preview tab which will show you the layer data in a map preview.
Right-clicking on a layer in the tree and clicking Add to Canvas will add this layer to your map.
So far we have only been viewing the database its schemas and tables and their metadata, but what if we wanted to
alter the table to add an additional column perhaps? DB Manager allows you to do this directly.
1. Select the table you want to edit in the tree
2. Select Table ► Edit Table from the menu, to open the Table Properties dialog.
You can use this dialog to Add Columns, Add geometry columns, edit existing columns or to remove a column
completely.
Using the Constraints tab, you can manage which fields are used as the primary key or to drop existing constraints.
The Indexes tab can be used to add and delete both spatial and normal indexes.
18.2. Lesson: Using DB Manager to work with Spatial Databases in QGIS 617
QGIS Training Manual
Now that we have gone through the process of working with existing tables in our database, let’s use DB Manager to
create a new table.
1. If it is not already open, open the DB Manager window, and expand the tree until you see the list of tables
already in your database.
2. From the menu select Table –> Create Table to bring up the Create Table dialog.
3. Use the default Public schema and name the table places.
4. Add the id, place_name, and elevation fields as shown below
5. Make sure the id field is set as the primary key.
6. Click the checkbox to Create geometry column and make sure it is set to a POINT type and leave it named
geom and specify 4326 as the SRID.
7. Click the checkbox to Create spatial index and click Create to create the table.
8. Dismiss the dialog letting you know that the table was created and click Close to close the Create Table Dialog.
You can now inspect your table in the DB Manager and you will of course find that there is no data in it. From here
you can Toggle Editing on the layer menu and begin to add places to your table.
The DB Manager will also let you do some basic database administration tasks. It is certainly not a substitute for a
more complete database administration tool, but it does provide some functionality that you can use to maintain your
database.
Database tables can often become quite large and tables which are being modified frequently can end up leaving
around remnants of records that are no longer needed by PostgreSQL. The VACUUM command takes care of doing
a kind of garbage collection to compact and optional analyze your tables for better performance.
Let us take a look at how we can perform a VACUUM ANALYZE command from within DB Manager.
1. Select one of your tables in the DB Manager Tree
2. Select Table ► Run Vacuum Analyze from the menu
PostgreSQL will now perform the operation. Depending on how big your table is, this may take some time to com-
plete.
You can find more information about the VACUUM ANALYZE process in the PostgreSQL Documentation on
VACUUM ANALYZE.
18.2. Lesson: Using DB Manager to work with Spatial Databases in QGIS 619
QGIS Training Manual
DB Manager also provides a way for you to write queries against your database tables and to view the results. We
have already seen this type of functionality in the Browser panel, but lets look at it again here with DB Manager.
1. Select the lines table in the tree.
2. Select the SQL window button in the DB Manager toolbar.
6. Click the checkbox for Load as new layer to add the results to your map.
7. Select the id column as the Column with unique integer values and the geom column as the Geometry column.
8. Enter roads_primary as the Layer name (prefix).
9. Click Load now! to load the results as a new layer into your map.
The layers that matched your query are now displayed on your map. You can of course use this query tool to execute
any arbitrary SQL command including many of the ones we looked at in previous modules and sections.
We have already looked at how to import data into a spatial database using command line tools, so now let’s learn
how to use DB Manager to do imports.
1. Click the Import layer/file button on the toolbar in the DB Manager dialog.
18.2. Lesson: Using DB Manager to work with Spatial Databases in QGIS 621
QGIS Training Manual
8. Dismiss the dialog letting you know that the import was successful
9. Click the Refresh button on the DB Manager Toolbar
You can now inspect the table in your database by clicking on it in the Tree. Verify that the data has been reprojected
by checking that the Spatial ref: is listed as WGS 84 (4326).
Right clicking on the table in the Tree and a selecting Add to Canvas will add the table as a layer in your map.
Of course DB Manager can also be used to export data from your spatial databases, so lets take a look at how that is
done.
1. Select the lines layer in the Tree and click the Export to File button on the toolbar to open the Export to
vector file dialog.
2. Click the … button to select the Output file and save the data to your exercise_data directory as ur-
ban_4326.
3. Set the Target SRID as 4326.
4. Click OK to initialize the export.
18.2. Lesson: Using DB Manager to work with Spatial Databases in QGIS 623
QGIS Training Manual
5. Dismiss the dialog letting you know the export was successful and close the DB Manager.
You can now inspect the shapefile you created with the Browser panel.
18.2.7 In Conclusion
You have now seen how to use the DB Manager interface in QGIS to manage your spatial databases, to execute SQL
queries against your data and how to import and export data.
Next, we will look at how to use many of these same techniques with SpatiaLite databases.
While PostGIS is generally used on a server to provide spatial database capabilities to multiple users at the same
time, QGIS also supports the use of a file format called SpatiaLite that is a lightweight, portable way to store an entire
spatial database in a single file. Obviously, these 2 types of spatial databases should be used for different purposes,
but the same basic principles and techniques apply to both. Let’s create a new SpatiaLite database and explore the
functionality provided to work with these databases in QGIS.
The goal for this lesson: To learn how to interact with SpatiaLite databases using the QGIS Browser interface.
Using the Browser panel, we can create a new SpatiaLite database and get it setup for use in QGIS.
1. Right click on the SpatiaLite entry in the Browser tree and select Create Database.
2. Specify where on your filesystem you want to store the file and name it qgis-sl.db.
3. Again right click on the SpatiaLite entry in the Browser tree and now select the New Connection item. Find the
file you created in the last step and open it.
Now that you have configured your new database you will find that the entry in Browser tree has nothing underneath
it and the only thing you can do at this point is to delete the connection. This is of course because we haven’t added
any tables to this database. Let’s go ahead and do that.
1. Find the button to create a new layer and use the dropdown to create a new SpatiaLite layer, or select Layer ►
New ► New SpatiaLite Layer.
2. Select the database we created in the previous steps in the drop down.
3. Give the layer the name places.
4. Tick the checkbox next to Create an auto-incrementing primary key.
5. Add two attributes as shown in below
6. Click OK to create the table.
7. Click the refresh button at the top of the Browser and you should now see your places table listed.
You can right click on the table and view its properties as we did in the previous exercise.
From here you can start an editing session and start adding data to your new database directly.
We also learned about how to import data into a database using the DB Manager and you can use this same technique
to import data into your new SpatiaLite DB.
18.3.2 In Conclusion
You have seen how to create SpatiaLite databases, add tables to them and use these tables as layers in QGIS.