grass-pg.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <!-- meta page description: PostgreSQL DATABASE DRIVER -->
  2. PostgreSQL database driver enables GRASS to store vector attributes in
  3. PostgreSQL server.
  4. <h2>Creating a PostgreSQL database</h2>
  5. A new database is created with <tt>createdb</tt>, see
  6. the <a href="http://www.postgresql.org/docs/manuals/">PostgreSQL
  7. manual</a> for details.
  8. <h2>Connecting GRASS to PostgreSQL</h2>
  9. <div class="code"><pre>
  10. # example for connecting to a PostgreSQL server:
  11. db.connect driver=pg database="host=myserver.osgeo.org,dbname=mydb"
  12. # password is asked interactively if not specified:
  13. db.login user=myname [pass=secret]
  14. db.connect -p
  15. db.tables -p
  16. </pre></div>
  17. <h2>Supported SQL commands</h2>
  18. All SQL commands supported by PostgreSQL.
  19. It's not possible to use C-like escapes (with backslash like \n etc)
  20. within the SQL syntax.
  21. <h2>Operators available in conditions</h2>
  22. All SQL operators supported by PostgreSQL.
  23. <h2>Adding an unique ID column</h2>
  24. Import vector module require an unique ID column which can
  25. be generated as follows in a PostgreSQL table:
  26. <div class="code"><pre>
  27. db.execute sql="ALTER TABLE mytable ADD ID integer"
  28. db.execute sql="CREATE SEQUENCE mytable_seq"
  29. db.execute sql="UPDATE mytable SET ID = nextval('mytable_seq')"
  30. db.execute sql="DROP SEQUENCE mytable_seq"
  31. </pre></div>
  32. <h2>Attribute import into PostgreSQL</h2>
  33. CSV import into PostgreSQL:
  34. <div class="code"><pre>
  35. \h copy
  36. COPY t1 FROM 'filename' USING DELIMITERS ',';
  37. </pre></div>
  38. <h2>Geometry import from PostgreSQL table into GRASS</h2>
  39. <em><a href="v.in.db.html">v.in.db</a></em> creates a new vector
  40. (points) map from a database table containing
  41. coordinates. See <a href="v.in.db.html">here</a> for examples.
  42. <h2>PostGIS: PostgreSQL with vector geometry</h2>
  43. <a href="http://postgis.refractions.net/">PostGIS</a>:
  44. adds geographic object support to PostgreSQL.
  45. <h3>Example: Import from PostGIS</h3>
  46. In an existing PostGIS database, create the following table:
  47. <div class="code"><pre>
  48. CREATE TABLE test
  49. (
  50. id serial NOT NULL,
  51. mytime timestamp DEFAULT now(),
  52. text varchar,
  53. wkb_geometry geometry,
  54. CONSTRAINT test_pkey PRIMARY KEY (id)
  55. ) WITHOUT OIDS;
  56. # insert value
  57. INSERT INTO test (text, wkb_geometry)
  58. VALUES ('Name',geometryFromText('POLYGON((600000 200000,650000
  59. 200000,650000 250000,600000 250000,600000 200000))',-1));
  60. # register geometry column
  61. select AddGeometryColumn ('postgis', 'test', 'geometry', -1, 'GEOMETRY', 2);
  62. </pre></div>
  63. GRASS can import this PostGIS polygon map as follows:
  64. <div class="code"><pre>
  65. v.in.ogr input="PG:host=localhost dbname=postgis user=neteler" layer=test \
  66. output=test type=boundary,centroid
  67. v.db.select test
  68. v.info -t test
  69. </pre></div>
  70. <h4>Geometry Converters</h4>
  71. <ul>
  72. <li><a href="http://postgis.refractions.net/download/">PostGIS with shp2pgsql</a>:<br>
  73. <tt>shp2pgsql -D lakespy2 lakespy2 test > lakespy2.sql</tt>
  74. </li>
  75. <li><a href="http://e00pg.sourceforge.net/">e00pg</a>: E00 to PostGIS filter,
  76. see also <em><a href="v.in.e00.html">v.in.e00</a></em>.
  77. </li>
  78. <li>GDAL/OGR <a href="http://www.gdal.org/">ogrinfo and ogr2ogr</a>:
  79. GIS vector format converter and library, e.g. ArcInfo or SHAPE to PostGIS.<br>
  80. <tt>ogr2ogr -f "PostgreSQL" shapefile ??</tt>
  81. </li>
  82. </ul>
  83. <h2>SEE ALSO</h2>
  84. <em>
  85. <a href="db.connect.html">db.connect</a>,
  86. <a href="db.execute.html">db.execute</a>
  87. </em>
  88. <p>
  89. <a href="databaseintro.html">Database management in GRASS GIS</a><br>
  90. <a href="database.html">Help pages for database modules</a><br>
  91. <a href="sql.html">SQL support in GRASS GIS</a><br>
  92. <h2>REFERENCES</h2>
  93. <ul>
  94. <li><a href="http://www.postgresql.org/">PostgreSQL web site</a></li>
  95. <li><a href="http://www.pgadmin.org/">pgAdmin graphical user interface</a></li>
  96. <li><a href="http://www.gdal.org/drv_pg.html">GDAL/OGR PostgreSQL
  97. driver documentation</a></li>
  98. </ul>
  99. <p>
  100. <i>Last changed: $Date$</i>