grass-pg.html 4.2 KB

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