35

As a bit of background, we are looking to transition away from the use of shapefiles. We've already started using Geopackages for desktop work (QGIS / ArcGIS) but often have a requirement to upload data to our PostgreSQL databases.

I'm aware that you are able to upload Geopackage content to Postgres using QGIS but wondering if there are any commandline utilities similar to shp2pgsql for uploading geopackages or tables in geopackages to Postgres? I'm assuming possibly ogr2ogr but can't find the documentation.

3 Answers 3

33

This tutorial shows how to import geopackges into postgres with ogr2ogr like:

ogr2ogr -f PostgreSQL "PG:dbname=gadm" DNK_adm.gpkg

Note importing into PostgreSQL like this will convert the case of the field names in your GeoPackage data to lowercase in the PostgreSQL table. To maintain case you would need to use the lco option like:

ogr2ogr -f PostgreSQL "PG:dbname=gadm" DNK_adm.gpkg -lco LAUNDER=NO
1
  • This is great. I've got ogr2ogr working with the .gpkg. I now need to do post-processing on each of the tables that's created from the gpkg in postgres in my commandline script and need to extract each table name from the gpkg. I can see ogrinfo list the tables but any ideas on how i can get these into a variable? Commented Jul 25, 2018 at 13:30
12

Using the tutorial on this link it´s better add to the GDAL command line (ogr2ogr) the next options.

ogr2ogr -f PostgreSQL "PG:user=youruser password=yourpassword dbname=yourdbname" yourgeopackage.gpkg

It works for me.

6

There is work ongoing in QGIS master (e.g. here and here to 'fix' the tool in QGIS (Import vector into Postgis database - ...) which generates command line parameters for OGR2OGR for you to use, specifically for Postgis, which seems to have broken with QGIS 3. I also have another pull request planned for more fixes and additional options. If you can wait a week or so, and use master, or 3 months or so and use 3.4 then that will avoid you having to input command line parameters manually, and should work for many different file formats, including geopackage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.