Back to GIS Tutorials
PostGIS

Add Shapefiles to PostGIS from QGIS

Import shapefiles into a PostGIS/PostgreSQL database using the SPIT tool in QGIS.

For adding shapefiles to a PostGIS database, follow these simple steps:

Step 1

A. Inside Database menu go to Spit option.
B. Click on Import Shapefiles to PostgreSQL.. A popup window titled SPIT — Shapefiles to PostGIS Import Tool will open.
Step 1 - SPIT menu

Step 2 — Make a new database connection

A. In SPIT window click on New for making new PostgreSQL connection.
Step 2A - New connection
B. A new window titled Create a New PostGIS connection will open.
Step 2B - Connection dialog
C. Give the connection name in the Name field. Here you can type any name e.g. myconnection or MyPGconnection etc.
D. Leave Service field blank.
E. In Host field enter localhost
F. Enter Port being used by Postgres — default port is 5432. But it’s better to confirm the port in pgAdmin III.
G. In Database field give the name of Postgres database.
H. Provide username and password used for connecting to Postgres database in pgAdmin.
I. Click on Test Connection button to test the connection. If connection is not successful then check the values entered in fields E to H. If connection is successful then click OK button.
J. The name of new connection will appear in SPIT window in PostgreSQL connections.

Step 3 — Connect to database and import

A. Select the new connection name and press Connect button.
B. In Geometry column name give any name like geom or use default the_geom.
C. In SRID field provide the correct SRID of the database (e.g. 4326 etc.)
D. In Primary key column name type gid
E. In Global Schema field use default value.
F. Click on Add button and navigate to the target folder and select the shapefile and click on Open button.
Step 3 - Add shapefile
G. The name of shapefile will be displayed in SPIT window under File Name. Here provide the table name (to be created in Postgres) in DB Relation Name option.
H. The Feature Class field should contain single part features. (Shapefiles containing Multipolygon/Multiline features should be converted into Polygon/Polyline features by using Multipart to Single Part Tool located in QGIS toolbox)
Step 3 - SPIT window with file
By pressing Add button more files can be added. After adding files press OK button in SPIT window. Check the database from pgAdmin.