22

How do I export a vector shapefile to PostGIS database? I am asking what format I have to export it.

2
  • 3
    You could use the shp2pgsql tool, included in postgis. See postgis.net/docs/manual-2.1/…
    – Oyvind
    Commented Aug 19, 2013 at 10:25
  • PostGIS database can import from a shapefile. are you asking how to export from some application in a format other than shapefile?
    – nmtoken
    Commented May 11, 2018 at 12:13

4 Answers 4

24

You can use the DBManager core plugin

enter image description here

3
  • please help me in that Query Builder question .i badly need answer for that only.........?
    – Arun
    Commented Aug 19, 2013 at 11:01
  • Is this something new? I don't see Import Layer & Export Layer Buttons in DB Manager v 0.1.20 in QGIS 1.8 on Windows Xp. Commented Aug 20, 2013 at 4:32
  • New in 2.0, well 1.9, until 2.0 is released.
    – Nathan W
    Commented Aug 20, 2013 at 4:33
16

As @oyvind suggested, shp2pgsql is the best method for just getting the data into a PostGIS database. OpenGEO has a good startup guide for loading data into PostGIS that should get you up and running if you're having issues. As @nathanw pointed out, DB Manager is a good option for importing a shapefile into a database. Also available is PostGIS Manager, which is a great option if you're working specifically with PostGIS. @underdark has a great blog post about how to use it.

You'll need the database connection information (username, hostname or host address, and database name) as well as the SRID for the shapefile, but that is all covered in the OpenGEO startup guide. Good luck!

2
  • 3
    DB Manger supports importing any layer that is open in QGIS not just shapefiles.
    – Nathan W
    Commented Aug 20, 2013 at 4:34
  • The DB manager link is broken, and the PostGIS Manger link leads to a page that says this is "depracated" in favor of DB Manager (which can handle SpatiaLite and PostGIS).
    – DarkCygnus
    Commented May 11, 2018 at 0:26
3

To export your vector file, with re-projection option using ogr2ogr, here is the command line:

ogr2ogr -overwrite -progress --config PG_USE_COPY YES -f PostgreSQL PG:"host=Your_host port=your_DB_port(5432) dbname=your_DB_name password=DB_password user=DB_user" dir_to_your_Vector(.shp, .gml, .geojson...) -nlt MULTIPOLYGON -lco SCHEMA=your_DB_schema(public) -lco GEOMETRY_NAME=geom -lco FID=id -nln your_Output_Table_Name -s_srs EPSG:your_Source_EPSG_code -t_srs EPSG:your_Target_EPSG_code

Don't forget to check that you have permissions to connect to the hose where the postgis is and to create the database.

1

Not a new answer, just an updated screen shot for Nathan W's excellent one. The DB manager interface is a bit different now (2.18) and the options are now in menus. The SQL window is under Database and the table related ones under table.

screed shot fro QGIS 2.18

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