grass-pg.html 4.4 KB

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