QGIS and PostGIS are excellent tools for managing geospatial data. QGIS is a full-featured GIS application with a graphical user interface, while PostGIS is a spatial extension for the PostgreSQL relational database management system. Together, they allow users to store, query, and visualise geospatial data, write spatial SQL queries, manage maps, and produce informative presentations.
This article discusses the process of connecting a PostGIS database with QGIS. It covers accessing, styling, and saving projects, as well as how to import, export, and write SQL queries directly in the QGIS interface.
Creating a Database Using PgAdmin
Key Points
- To create a database using PgAdmin: Databases >> Create >> Database >> set database name >> set the owner >> Save.
- To create a spatial extension: Extensions >> Create >> Extension >> set name – PostGIS >> Save.
- To create a new PostGIS connection in QGIS: Layer >> Add Layer >> Add PostgreSQL Layer >> New.
- To join two datasets/tables, from the Layer panel: Spatial table >> Joins >> select both tables >> set Target field.
- To write a query, open the SQL Window, write the query, then click Execute to run it.
- To save query results as a layer: check Load as new layer >> set your geometry column and unique column >> Load.
- To save a style as default, in the layer panel: right-click the layer >> Properties >> Symbology >> Style >> Save as Default >> In Database (Data Source).
- To save a project to the PostGIS database: Project >> Save To >> PostgreSQL >> set database name, schema, and project name >> OK.
How to Create a Database Using PgAdmin
Creating a PostgreSQL database can be done using the command line or using the PgAdmin graphical user interface, depending on preference.
To create a spatial database using PgAdmin, enter the master password in the dialogue to access PostgreSQL.
In PgAdmin: 1Databases (right-click) >> 2Create >> 3Database >> 4set database name >> 5set owner (the default superuser, postgres, is fine) >> 6Save. [The image below and subsequent images are numbered according to the superscripts in the descriptions.]
1 Create a PostgreSQL database using PgAdmin
To create a spatial extension: 1Extensions (right-click) >> 2Create >> 3Extension >> 4set name – PostGIS >> 5Save.
2 Add a PostGIS spatial extension to a PostgreSQL database
Confirm this worked by checking that the spatial_ref_sys table has been created in the database: Schemas >> Tables >> spatial_ref_sys.
The spatial_ref_sys table holds definitions for thousands of coordinate reference systems (CRS), identified by their SRID (Spatial Reference ID). Its presence confirms that the PostGIS extension was installed successfully. The database is now spatially enabled and ready to connect to QGIS.
How to Create a New PostGIS Connection in QGIS
A new PostGIS connection can be created in several ways:
1. 1Layer >> 2Add Layer >> 3Add PostgreSQL Layer >> 4New
2. AData Source Manager (on the toolbar) >> BPostgreSQL >> CNew
3. iBrowser panel >> iiPostgreSQL (right-click) >> iiiNew Connection
3 Create a new PostGIS connection in QGIS
In the connection dialogue, set the following parameters:
Name → preferred identifier for the connection
Host → localhost
Port → 5432 (default) – adjust only if a different port was used when setting up the database.
Database → database name for the connection
4 Create a new PostGIS connection in QGIS
Authentication can be configured or left at the basic level. Note that the basic option stores the password in plain text in the QGIS project file, which can be read in a text editor. For greater security, use an authentication configuration.
To set up an authentication configuration, click the “+” icon >> enter a master password
>> Fill in the authentication information with the username and password
>> Test Connection
5 Set authentication configuration for the QGIS–PostGIS connection
It is also useful to enable the option to list all tables, including those without a geometry column, and to allow syncing projects between QGIS and the database.
6 Create a new PostGIS connection in QGIS
>> OK.
When working in an enterprise environment, the auto-generated authentication configuration ID is used for verification before connecting to a database. It can be accessed using the edit configuration button or through the Authentication tab in Settings.
To reset the master password, delete all previous configurations from the QGIS authentication database:
1Settings >> 2Options >> 3Authentication >> 4Utilities >> 5Erase Authentication Database
7 Reset the database authentication password
Working With Datasets – QGIS and PostGIS Database
Using the QGIS Database Manager tool, the user can view and modify tables, schemas, and other objects in the database.
1. Quick Features
- Create, rename, and delete schemas and tables.
- Delete all features from a table by truncating it.
- Export a table to different file formats.
- Edit tables – delete columns, change column names and types, add a geometry column to a non-spatial table, add a spatial index, and more.
- Import and export data files in any QGIS-supported file format.
8 Access files using the QGIS Database Manager tool
2. Import Datasets
All datasets loaded in the QGIS workspace will be listed as options when importing files to the database. Otherwise, navigate to the file directory.
>> Select the target schema >> optionally rename the primary key and geometry column, and verify the Spatial Reference ID (SRID, auto-detected). If datasets were received in different SRIDs, set the Target SRID to ensure all datasets align.
9 Import datasets into the PostGIS database in QGIS
The time taken will depend on the size of the dataset.
3. Join Datasets
When the attributes of a dataset are split across two or more tables, performing joins is necessary to view all attributes together.
To join two datasets/tables (spatial and non-spatial):
From the Layer panel: Spatial table (double-click) >> Joins >> select both tables >> set Target field to a field that is common to both tables.
Enable the “Editable join layer” option if edits to the attributes of both tables are required.
10 Join datasets in QGIS
This setting also helps synchronise changes when features are added or deleted.
4. Run Queries
The DB Manager tool allows users to run SQL queries directly in QGIS. Ensure the database is connected by expanding its entry in the DB Manager tree.
To write a query, open the 1SQL Window and use the provided editor. Click 2Execute to run the query. Results can be saved as a new layer, exported to a file, saved as a named query, or created as a view in the QGIS workspace.
To save query results as a layer: 3check “Load as new layer” >> set the 4geometry column and unique column >> 5Load.
11 Write spatial SQL queries in the QGIS DB Manager tool
5. Setting up Styles
Styling GIS maps is important when presenting analysis results. The DB Manager tool allows users to copy, apply, and store map styles (symbology, labels, and forms) from previously generated maps for reuse in future projects.
To copy a map style:
In the layer panel, right-click the layer >> Styles >> Copy.
Select the specific style categories of interest, or choose “All style categories” to copy all feature styles. Apply the style to another layer by pasting it in the style menu.
12 Copy and paste map styles between QGIS layers
To save a style as default:
In the layer panel, right-click the layer >> Properties >> Symbology >> Style >> Save as Default >> In Database (Data Source).
13 Save a map style as default
To save a style and manually apply it to specific features of another layer, use the “Save Style” option under Properties >> Symbology >> Style >> Save Style.
The style is stored in the layer_styles table in the public schema of the database.
This feature is particularly useful in enterprise environments where multiple users work on the same or similar mapping projects, as it maintains visual consistency across projects.
6. Save QGIS Projects to a PostGIS Database
Saving projects to the database allows different users easy access to the most current version of a project.
To save a project to the PostGIS database:
Project >> Save To >> PostgreSQL >> set database name, schema, and project name >> OK.
14 Save a QGIS project to a PostgreSQL database





