grass-pg.html 3.7 KB

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