grass-pg.html 4.5 KB

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