25.2.1. DB Manager Plugin

The DB Manager Plugin is intended to be the main tool to integrate and manage spatial database formats supported by QGIS (PostGIS, SpatiaLite, GeoPackage, Oracle Spatial, Virtual layers) in one user interface. The dbManager DB Manager Plugin provides several features. You can drag layers from the QGIS Browser into the DB Manager, and it will import your layer into your spatial database. You can drag and drop tables between spatial databases and they will get imported.

../../../../_images/db_manager1.png

Fig. 25.8 DB Manager dialog

The Database menu allows you to connect to an existing database, to start the SQL window and to exit the DB Manager Plugin. Once you are connected to an existing database, the menus Schema (relevant for DBMSs, such as PostGIS / PostgreSQL) and Table will appear.

The Schema menu includes tools to create and delete (only if empty) schemas and, if topology is available (e.g. with PostGIS topology), to start a TopoViewer.

The Table menu allows you to create and edit tables and to delete tables and views. It is also possible to empty tables and to move tables between schemas. You can Run Vacuum Analyze for the selected table. Vacuum reclaims space and makes it available for reuse, and analyze updates statistics that is used to determine the most efficient way to execute a query. Change Logging… allows you to add change logging support to a table. Finally, you can Import Layer/File… and Export to File….

The Providers window lists all existing databases supported by QGIS. With a double-click, you can connect to the database. With the right mouse button, you can rename and delete existing schemas and tables. Tables can also be added to the QGIS canvas with the context menu.

If connected to a database, the main window of the DB Manager offers four tabs. The Info tab provides information about the table and its geometry, as well as about existing fields, constraints and indexes. It allows you to create a spatial index on a the selected table. The Table tab shows the table, and the Preview tab renders the geometries as preview. When you open an SQL Window, it will be placed in a new tab.

25.2.1.1. Working with the SQL Window

You can use the DB Manager to execute SQL queries against your spatial database. Queries can be saved and loaded, and there the SQL Query Builder will help you formulate your queries. You can even view spatial output by checking Load as new layer and specifying Column(s) with unique values (IDs), Geometry column and Layer name (prefix). It is possible to highlight a portion of the SQL to only execute that portion when pressing Ctrl+R or clicking the Execute button.

The Query History button stores the last 20 queries of each database and provider.

Double clicking on an entry will add the string to the SQL window.

../../../../_images/db_manager_sql.png

Fig. 25.9 Executing SQL queries in the DB Manager SQL window

Muista

The SQL Window can also be used to create Virtual Layers. In that case, instead of selecting a database, select QGIS Layers under Virtual Layers before opening the SQL Window. See Creating virtual layers for instructions on the SQL syntax to use.