48

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?

3 Answers 3

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.

0
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.

0
9

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

enter image description here

0

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.