Using GDAL to load a GeoPackage into a database
GDAL is a translator library for raster and vector geospatial data formats that is released under an X/MIT style Open Source License by the Open Source Geospatial Foundation. It comes with a variety of useful command line utilities for data translation and processing. The following section covers the loading of GeoPackage datasets into a PostgreSQL database using the ETL tool GDAL. The process will be similar for other databases such as Oracle and SQL Server, as well as converting to other data formats.
A PostgreSQL database with PostGIS extension enabled
GDAL version 1.11.0 or above (with access to a command line interface to use it)
A GeoPackage dataset
You can interrogate a GeoPackage dataset using the ogrinfo program which lists information about it. At a basic level it will return the layers contained within it:
ogrinfo
<PATH_TO_GEOPACKAGE>
Using the ‘Summary Only’ (-so
) and ‘List all features of all layers’ (-al
) arguments you can view summary information about all the layers within the GeoPackage, including projection, schema, feature count and extents:
ogrinfo
<PATH_TO_GEOPACKAGE>
-so -al
The GeoPackage can be loaded into a PostgreSQL database using the ogr2ogr program, the below will load all layers from the source GeoPackage into the specified target schema in the database:
ogr2ogr -f PostgreSQL "PG:user=
<USERNAME>
password=
<PASSWORD>
dbname=
<DATABASENAME>
host=
<HOST>
port=
<PORTNUMBER>
active_schema=
<TARGETSCHEMA>
"
<PATHTOGEOPACKAGE>
<USERNAME>, <PASSWORD>, <DATABASENAME>, <HOST>, <PORTNUMBER> are the connection details of the target PostgreSQL database.
<TARGETSCHEMA> is the schema in the database that the layers should be loaded into. If this doesn’t exist or if it is omitted, they will be loaded into the default schema, the default is usually the ‘public’ schema.
Will create two tables in the example_schema
schema:
Different loading options (including renaming tables, reprojecting the data, etc.) can be found on the PostgreSQL / PostGIS — GDAL documentation page.