GIS Tutorials >> Quantum GIS >> Intermediate Quantum GIS Tutorials >> Working with your PostGIS Layers using Quantum GIS (QGIS)

GIS Programming Tutorials

GIS Theoretical Tutorials

GISTutor Newsletter

GISTutor Poll

How did you hear about us?

GISTutor Users Online

We have 67 guests online
Working with your PostGIS Layers using Quantum GIS (QGIS)
( 8 Votes )



This tutorial will explain to the user how to connect QGIS to your PostgreSQL/PostGIS database and spatially view any of your tables containing a geometry column. PostGIS is a powerful database for GIS functions and data storage so it is nice to have a program to visually see the GIS data stored within the database.  QGIS is an easy to use open source GIS application and is ideal for data viewing, editing, and making maps.


You are required to have a PostgreSQL database with PostGIS installed.  This database will need a GIS table (contains a geometry type column).  Start by downloading a sample shapefile here and follow the next few steps to import the shapefile to PostgreSQL using QGIS.


Importing Shapefile Data to PostGIS using QGIS

Open Quantum GIS and click the “Import Shapefiles to PostgreSQL" QGIS Import Shapefiles to PostgreSQL button. This will open SPIT (Shapefile to PostGIS Import Tool).


Start by creating a connection to your PostgreSQL database.  Under “PostgreSQL Connections” click the “New” button.  Add a name for your connection, and then enter the parameters for your database connection (Host, Database, Port, Username, and Password).  Click the “Test Connection” button and you should see a message saying “Connection to databasename was successful”.  Once this is successful, click OK.


QGIS PostGIS Connection


Select your server from the dropdown list and click “Connect”, a list of your schemas will appear in the Global schema dropdown menu.  Choose the schema where you would like to import the data. Now click the “Add” button and add your shapefile. Ensure the information is correct concerning the shapefile and click “OK”.


QGIS SPIT (Shapefile to PostGIS Import Tool)


Your shapefile is now added to your database. Next, we will view the newly added PostGIS data.


Viewing PostGIS Layers using QGIS

Click the “Add PostGIS Layer” button QGIS Add PostGIS Layer. This will open up a window.


Under “PostgreSQL Connections” select your server name from the dropdown list and click “Connect”.  You will see a list of your PostgreSQL schemas. Open the schema containing your GIS data to see a list of all available layers.  Select the n_america layer click “Add”.


QGIS Add PostGIS Table


The GIS data for North America will appear in the data viewing area. Use the “Zoom In” tool QGIS Zoom In to draw a box closely around the US, Canada, and Mexico.


QGIS Layer View


To view the attribute table for this layer, right-click the layer and choose “Open Attribute Table” or click the icon QGIS Open Attribute Table when the layer is selected.


To edit your data, turn on the editor by clicking the “Toggle Editing” icon QGIS Toggle Editing.  Save your edits using the “Save Edits” button QGIS Save Edits.


A simple way to identify features or query the attributes of a location is to use the “Identify Features” tool QGIS Identify Features. Click a location on the map to view its attributes.


Classifying your Data


Right-click the n_america layer and choose “Properties”. Under Symbology -> Legend Type, choose “Unique Value”. Under “Classification field”, choose “country” and click Classify. Each unique value in the country column will be given a random color. Click “OK”.


QGIS Symbology


Now let’s add some labels.  Select the n_america layer and click the “Labeling” button QGIS Labeling. Put a check next to “Label this layer”. Under “Field with labels”, choose “country” and click “OK”.




This concludes our tutorial on working with PostGIS data with QGIS. The user should now know how to import shapefile data into a PostGIS database, load that layer in QGIS, view/edit the attributes, and symbolize the map according to its values.



0 #3 hdh 2013-05-03 14:27
How do I add a "gis table" to PostgreSQL? Unsure what this means / how to do it.

+2 #2 Rado Miarintsoa 2013-04-17 09:16
I work at the Madagascar Land Observatory. We used the GIS and we try to publish it on line. I want to import the shapefile database to PostgreSQL using QGIS. I tried several times to connect with the database PostgreSQL but I can't,Could you help me please! Rado
0 #1 2012-05-05 00:13
Can you add more detail to the passage below? I had to select the "also list tables with no geometry" to even see a list of schemas & then had no idea which one to choose. - e.g. which schema should be selected?

...a list of your schemas will appear in the Global schema dropdown menu. Choose the schema where you would like to import the data...

Add comment

Security code