Database

Export to PostgreSQL

Exports a vector layer to a PostgreSQL database. Prior to this a connection between QGIS and the PostgreSQL database has to be created (see eg Creating a stored Connection).

Parameters

Layer to import [vector: any]
Vector layer to add to the database.
Database (connection name) [string]
Name of the database connection (not the database name). Existing connections will be shown in the combobox.
Schema (schema name) [string]

Optional

Name of the schema to store the data. It can be a new one or already exist.

Default: public

Table to import to (leave blank to use layer name) [string]

Optional

Defines a table name for the imported vector file. If nothing is added, the layer name will be used.

Primary key field [tablefield: any]

Optional

Sets the primary key field from an existing field in the vector layer. A column with unique values can be used as Primary key for the database.

Geometry column [string]

Defines the name of the geometry column in the new PostGIS table. Geometry information for the features is stored in this column.

Default: geom

Encoding [string]

Optional

Defines the encoding of the layer in the new PostGIS table.

Default: UTF-8

Overwrite [boolean]

Overwrites existing tables having the same name.

Default: True

Create spatial index [boolean]

Specifies whether to create a spatial index or not.

Default: True

Convert field names to lowercase [boolean]

Converts the field names of the input vector layer to lowercase.

Default: True

Drop length constraints on character fields [boolean]

Specifies whether the length constraints on character fields should be dropped or not.

Default: False

Create single-part geometries instead of multi-part [boolean]

Specifies if the features of the loaded layer should be single-part instead of multi-part. By default the existing geometries information are preserved.

Default: False

Export to SpatiaLite

Exports a vector layer to a SpatiaLite database. Prior to this a connection between QGIS and the SpatiaLite database has to be created (see eg SpatiaLite Layers).

Parameters

Layer to import [vector: any]
Vector layer to add to the database.
File database [vector]
The SQLite/SpatiaLite database file to connect to.
Table to import to (leave blank to use layer name) [string]

Optional

Defines a table name for the imported vector file. If nothing is added, the layer name will be used.

Primary key field [tablefield: any]

Optional

Sets the primary key field from an existing field in the vector layer.

Geometry column [string]

Defines the name of the geometry column in the new SpatiaLite table. Geometry information for the features is stored in this column.

Default: geom

Encoding [string]

Optional

Defines the encoding of the layer in the new SpatiaLite table.

Default: UTF-8

Overwrite [boolean]

Overwrites existing table having the same name.

Default: True

Create spatial index [boolean]

Specifies whether to create a spatial index or not.

Default: True

Convert field names to lowercase [boolean]

Converts the field names of the input vector layer to lowercase.

Default: True

Drop length constraints on character fields [boolean]

Specifies whether the length constraints on character fields should be dropped or not.

Default: False

Create single-part geometries instead of multi-part [boolean]

Specifies if the features of the loaded layer should be single-part instead of multi-part. By default the existing geometries information are preserved.

Default: False

Package layers

Collects a number of existing layers and packages them together into a single GeoPackage database.

Parameters

Input layers [vector: any] [list]
All the vector layers to import into the GeoPackage database.
Overwrite existing GeoPackage [boolean]

Replaces an existing database with a new one.

Default: False

Outputs

Destination GeoPackage
If not specified the GeoPackage database will be saved in the temporary folder.

PostgreSQL execute and load SQL

Allows a SQL database query to be performed on a PostgreSQL database connected to QGIS and loads the result. The algorithm won’t create any new layer: it is designed to run queries on the layer itself.

Example

1. Set all the values of an existing field to a fixed value. The SQL query string will be:

UPDATE your_table SET field_to_update=20;

in the example above, the values of the field field_to_update of the table your_table will be all set to 20.

2. Create a new area column and calculate the area of each feature with the ST_AREA PostGIS function.

Run the first query and create the new column area on the table my_table:

ALTER TABLE my_table ADD COLUMN area double precision;

Run the second query and update the area column and calculate the area of each feature:

UPDATE my_table SET area=ST_AREA(geom);

Parameters

Database (connection name) [string]
Name of the database connection (not the database name). Existing connections will be shown in the combobox.
SQL query [string]
Defines the SQL query, for example UPDATE my_table SET field=10.
Unique ID field name [tablefield: any]

Sets the primary key field from an existing field in the table.

Default: id

Geometry column [string]

Optional

Name of the geometry column in the table.

Default: geom

Outputs

No new layer is created. The SQL query is executed in place on the layer and its result (as a subset of the input table) is automatically loaded in QGIS.

PostgreSQL execute SQL

Allows a SQL database query to be performed on a PostgreSQL database connected to QGIS. The algorithm won’t create any new layer: it is designed to run queries on the layer itself.

Parameters

Database [string]
Name of the database, not the connection name. By default you don’t have to fill in the name, the current database connection will be chosen.
SQL query [string]
Defines the SQL query, for example UPDATE my_table SET field=10.

Outputs

No new layer is created. The SQL query is executed in place on the layer.

See also

For some SQL query examples see PostGIS SQL Query Examples.

SpatiaLite execute SQL

Allows a SQL database query to be performed on a SpatiaLite database connected to QGIS. The algorithm won’t create any new layer: it is designed to run queries on the layer itself.

Parameters

Database [vector]

The SQLite/SpatiaLite database file to connect to.

Default: (not set)

SQL query [string]

Defines the SQL query, for example UPDATE my_table SET field=10.

Default: (not set)

Outputs

No new layer is created. The SQL query is executed in place on the layer.

See also

For some SQL query examples see PostGIS SQL Query Examples.