47

I was able to create a Postgresql database and then I executed,

createlang plpgsql geospecies
psql -d geospecies -f postgis.sql
psql -d geospecies -f spatial_ref_sys.sql

So now can I directly import the shapefiles?

Would this be the right command?

ogr2ogr -f PostgreSQL PG:"host=server_ip user=username dbname=dbname password=password" yourshapefile.shp;

I am not sure how to structure the database, would it not need a table?

I will eventually need to add a bunch load of shapefiles and so is there a command to do that?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Sam007
  • 4,395
  • 7
  • 48
  • 69

3 Answers3

68

Try shp2pgsql. The basic syntax is like:

shp2pgsql -s SRID SHAPEFILE.shp SCHEMA.TABLE | psql -h HOST -d DATABASE -U USER

I always find this cheatsheet from http://www.bostongis.com useful. If you scroll down a little, you will find simple examples on how to load data.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
9

Anyone doing this in or after 2021 should be aware of the QGIS Processing Toolbox tool 'Export to PostgreSQL':

enter image description here

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Encomium
  • 3,133
  • 2
  • 14
  • 41
9

TABLE is just whatever you want to name it and would be the name to use when calling it from a query, the table will be generated upon import.

If you become lazy like me, I just use the SPIT (Shapefile to PostGIS Import Tool) available as a QGIS plugin to import all my shapefiles now. Assuming you are using QuantumGIS as you said you were using OpenGeo earlier.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
RomaH
  • 2,745
  • 13
  • 19