• Posts
  • Spatial Lab
  • Modern GIS Accelerator
  • The Spatial SQL Book

Get the newsletter

Join 71,000+ geospatial experts growing their skills and careers. Get updates on the most cutting edge updates in modern GIS and geospatial every week.

Edit Content
  • LinkedIn
  • YouTube
Tutorial

SQL Basics for Spatial SQL: Importing Data to PostGIS

March 23, 2022 Matt Forrest Comments Off on SQL Basics for Spatial SQL: Importing Data to PostGIS

If you are looking for help loading a variety of geospatial data into PostGIS, then you have landed in the right place. This is a guide for my video below with further tutorials and guides. Special shout out to the team at Crunchy Data for their fantastic post on the topic.

The Spatial SQL Book – Available now!

Check out my new book on Spatial SQL with 500+ pages to help you go from SQL novice to spatial SQL pro.

Buy now!

shp2pgsql

This is a command-line function that comes with PostGIS that allows you to import Shapefiles into a PostGIS database directly.

Example

shp2pgsql -s 4326 -I \
./Parking_Garages_and_Lots.shp \
> parking_garages_lots.sql

# Then run...

psql -U user -h localhost \
-d states -f states.sql

# Or all in one line

shp2pgsql -s 4326 \
-I ./states.shp \
| psql -U user -h localhost states

Resources

Loading Data into PostGIS: An Overview
There are a lot of ways to load data into a PostgreSQL/PostGIS database and it’s no different with spatial data. If you’re new to PostGIS, you’ve come to the right place. In this blog post, I’ll outline a few free, open source tools you can use for your spatial data import needs.
Chapter 4. Data Management
4.1. Spatial Data Model

ogr2ogr

Another way to do this is to use OGR2OGR, or the vector to vector file translation toolkit from GDAL that allows you to translate one of many different file types into a SQL file and also import it directly to your database.

Example

ogr2ogr \
-f PostgreSQL PG:"host=localhost user=kat password=password \
dbname=states" ./states.geojson \
-nln states -lco GEOMETRY_NAME=geom

Resources

Loading Data into PostGIS: An Overview
There are a lot of ways to load data into a PostgreSQL/PostGIS database and it’s no different with spatial data. If you’re new to PostGIS, you’ve come to the right place. In this blog post, I’ll outline a few free, open source tools you can use for your spatial data import needs.
Using shp2pgsql instead of ogr2ogr to import shapefile to PostGIS?
I may very well be doing something wrong here, but: If I import some shapefile to a PostGIS database using shp2pgsql, I have to first figure out the SRID/EPSG of that shapefile. I think this is, at

PostgreSQL COPY

You can also use the native COPY command for loading CSV data directly into your database, although you will need to use GDAL and OGR2OGR to turn your data into a CSV if it is not already.

Example

# Turn your files into a CSV

ogr2ogr -f CSV file.csv file.shp

# Create your new table in the database

CREATE TABLE states (
    geom GEOMETRY,
    name TEXT)

# COPY the file

COPY states FROM './file.csv' (DELIMITER ' ,')

Resources

PostgreSQL copy command Example
Loading Data into PostGIS: An Overview
There are a lot of ways to load data into a PostgreSQL/PostGIS database and it’s no different with spatial data. If you’re new to PostGIS, you’ve come to the right place. In this blog post, I’ll outline a few free, open source tools you can use for your spatial data import needs.

QGIS

If you are not as familiar with the command line and want a familiar interface I recommend using QGIS with your database. It is super simple and gives you a familiar desktop environment to upload your data.

Example

PostGIS and QGIS in 4 minutes

Resources

Spatial Database: Connect to PostgreSQL/PostGIS Through QGIS
Loading Data into PostGIS: An Overview
There are a lot of ways to load data into a PostgreSQL/PostGIS database and it’s no different with spatial data. If you’re new to PostGIS, you’ve come to the right place. In this blog post, I’ll outline a few free, open source tools you can use for your spatial data import needs.

GeoPandas

Finally, you can use GeoPandas to not only read data from PostGIS but also import your GeoDataFrames into a PostGIS database.

Example

from sqlalchemy import create_engine

engine = create_engine("postgresql://user:pass@host:5432/db")

gdf.to_postgis("my_table", engine)

Resources

geopandas.GeoDataFrame.to_postgis — GeoPandas 0.10.2+0.g04d377f.dirty documentation
Upload GeoDataFrame into PostGIS database.
PostGIS and GeoPandas in Jupyter in 4 minutes
  • Spatial SQL
Matt Forrest

Post navigation

Previous
Next

Search

Categories

  • Article (23)
  • Tutorial (5)

Recent posts

  • Geospatial Tools Compared: When to Use GeoPandas, PostGIS, DuckDB, Apache Sedona, and Wherobots
  • Scaling GIS Workflows with COGs, Airflow, and Apache Iceberg
  • Spatial Joins: A comprehensive guide

Tags

aggregations Apache Airflow Apache Iceberg Apache Sedona bigquery Cloud GIS Cloud Optimized GeoTIFF duckdb geoparquet geospatial gis Modern GIS postgis Python snowflake Spatial SQL sql zip codes

Related posts

Article

Scaling GIS Workflows with COGs, Airflow, and Apache Iceberg

April 25, 2025 Matt Forrest Comments Off on Scaling GIS Workflows with COGs, Airflow, and Apache Iceberg

TOP OF THE STACK What we need to do with COGs COGs (Cloud-Optimized GeoTIFFs) are one of the most promising tools we have for making raster data truly cloud-native. They let you stream just the pieces you need, work remotely, and plug into modern geospatial systems without downloading giant files. But after working closely with […]

Article

From Desktop GIS to Cloud: A Beginner’s Roadmap to Modern GIS Tool

March 7, 2025 Matt Forrest Comments Off on From Desktop GIS to Cloud: A Beginner’s Roadmap to Modern GIS Tool

Modern GIS is changing fast. If you’ve been working with QGIS, ArcGIS, or any other desktop GIS tool, you’ve probably hit some limitations—datasets getting too big, processing times slowing down, and collaboration becoming a challenge. The good news? The cloud offers a way forward. But how do you make that transition? How do you go […]

Article

The Top 11 Open GeoParquet Datasets: Making big geospatial data easy

January 18, 2024 Matt Forrest Comments Off on The Top 11 Open GeoParquet Datasets: Making big geospatial data easy

In the dynamic field of geospatial technology, the evolution of data formats plays a pivotal role in shaping how we interact with and interpret spatial information. The advent of GeoParquet has marked a significant milestone, offering a more efficient and accessible way to handle large spatial datasets. This blog post delves into a comprehensive exploration […]

Spatial Lab
  • Join the Spatial Lab community
Courses
  • Learn Modern GIS with courses and certifications
Spatial SQL
  • Get the Spatial SQL book today
Join Us

© Matt Forrest 2024. All Rights Reserved.