10

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.

7

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

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

  • 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? – James Norris Jul 25 '18 at 13:30
3

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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