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