grass-pg.html 4.4 KB

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