# GDAL

[GDAL](https://gdal.org) is a translator library for raster and vector geospatial data formats that is released under an X/MIT style Open Source [License](https://gdal.org/license.html#license) by the [Open Source Geospatial Foundation](http://www.osgeo.org). 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](https://www.postgresql.org) 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.

## Requirements

* A [PostgreSQL](https://www.postgresql.org) database with [PostGIS](https://postgis.net) extension enabled
* GDAL version 1.11.0 or above (with access to a command line interface to use it)
* A GeoPackage dataset

## Instructions

{% stepper %}
{% step %}

### Use the [ogrinfo](https://gdal.org/programs/ogrinfo.html) program to list information about the GeoPackage

`ogrinfo`` `**`<PATH_TO_GEOPACKAGE>`**&#x20;

{% code title="Example:" %}

```
ogrinfo wtr_ntwk_waterlinkset.gpkg
```

{% endcode %}

{% code title="ogrinfo output" %}

```
INFO: Open of `C:\wtr_ntwk_waterlinkset.gpkg'
      using driver `GPKG' successful.
1: wtr_ntwk_waterlinkset
2: wtr_ntwk_waterlinkset_wtrlinkref (None)
```

{% endcode %}

Without any arguments supplied, ogrinfo will return the layers contained within the GeoPackage.
{% endstep %}

{% step %}

### Use ‘Summary Only’ (`-so`) and ‘List all features of all layers’ (`-al`) arguments to view summary information about the layers within the GeoPackage

`ogrinfo`**`<PATH_TO_GEOPACKAGE>`**`-so -al`&#x20;

{% code title="Example:" %}

```
ogrinfo wtr_ntwk_waterlinkset.gpkg -so -al
```

{% endcode %}

{% code title="ogrinfo detailed output" %}

```
INFO: Open of `C:\wtr_ntwk_waterlinkset.gpkg'
      using driver `GPKG' successful.
Layer name: wtr_ntwk_waterlinkset
Geometry: Unknown (any)
Feature Count: 116
Extent: (209562.134000, 79229.510000) - (341688.834000, 853194.875000)
Layer SRS WKT:
PROJCRS["OSGB 1936 / British National Grid",
[...]
```

{% endcode %}

Combined, these arguments will provide summary information about all the layers within the GeoPackage, including projection, schema, feature count and extents.
{% endstep %}

{% step %}

### Load the GeoPackage into a PostgreSQL database using the [ogr2ogr](https://gdal.org/programs/ogr2ogr.html) program

The arguments 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>`**

{% hint style="info" %}
**\<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.
{% endhint %}

```
ogr2ogr -f PostgreSQL "PG:user=example_user password=example_password dbname=postgres host=localhost port=5432 active_schema=example_schema" wtr_ntwk_waterlinkset.gpkg
```

This will create twp tables in the `example_schema` schema:

```
schemaname     | tablename
---------------+---------------------------------
example_schema | wtr_ntwk_waterlinkset
example_schema | wtr_ntwk_waterlinkset_wtrlinkref
```

{% endstep %}
{% endstepper %}

Different loading options (including renaming tables, reprojecting the data, etc.) can be found on the [PostgreSQL / PostGIS — GDAL documentation](https://gdal.org/drivers/vector/pg.html) page.
