grass-pg(1)

πŸ’– grass-pg β€” PostgreSQL DATABASE DRIVER πŸ’–

okay bestie, gather round, because this one is literally the moment GRASS GIS links arms with PostgreSQL and stores all your vector attributes on a whole actual database server. this is grass-pg(1) from the GRASS GIS User's Manual and it's giving main character energy fr fr πŸ¦‹


✨ PostgreSQL DATABASE DRIVER ✨

PostgreSQL database driver = the thing that lets GRASS keep your vector attributes inside a PostgreSQL server instead of just chillin' in a local file. iconic behavior, no cap. πŸ‘‘


🌸 Creating a PostgreSQL database 🌸

a brand new database gets born via createdb. that's it, that's the vibe β€” go peek at the PostgreSQL manual if you want the deets, because it's all theirs. πŸ’…


πŸŽ€ Connecting GRASS to PostgreSQL πŸŽ€

here's the glow-up sequence, watch closely:

# example for connecting to a PostgreSQL server:
db.connect driver=pg database=mydb
db.login user=myname password=secret host=myserver.osgeo.org  # port=5432
db.connect -p
db.tables -p

that's connect β†’ login β†’ vibe check the connection β†’ vibe check the tables. we love a little pipeline. ✨

πŸ” Username and password πŸ”

straight from the PostgreSQL manual, bestie, quoting it so you don't have to go hunting:

The file .pgpass in a user's home directory can contain passwords to be used if the connection requires a password (and no password has been specified otherwise). On Microsoft Windows the file is named %APPDATA%\postgresql\pgpass.conf (where %APPDATA% refers to the Application Data subdirectory in the user's profile). Alternatively, a password file can be specified using the connection parameter passfile or the environment variable PGPASSFILE. This file should contain lines of the following format:
hostname:port:database:username:password

put that line in a file, protect it, and your password never has to show up in your shell history again. we stan a discreet queen. πŸ€«πŸ’–


🧚 Supported SQL commands 🧚

All SQL commands that PostgreSQL supports. all of them. the whole menu. 😌

⚠️ but bestie, listen: it is not possible to use C-like escapes (the backslash ones like \n etc) inside the SQL syntax. so don't try it, she will not eat. πŸ’€

🌈 Operators available in conditions 🌈

All SQL operators that PostgreSQL supports. yes queen, every single one. πŸ‘‘


πŸ“ Adding an unique ID column πŸ“

import vector modules need an unique ID column, and you can manifest one in a PostgreSQL table like this:

db.execute sql="ALTER TABLE mytable ADD ID integer"
db.execute sql="CREATE SEQUENCE mytable_seq"
db.execute sql="UPDATE mytable SET ID = nextval(’mytable_seq’)"
db.execute sql="DROP SEQUENCE mytable_seq"

add the column, spin up a sequence, fill it in, then yeet the sequence. clean. ✨


πŸ’œ Attribute import into PostgreSQL πŸ’œ

CSV import into PostgreSQL goes a little something like:

\h copy
COPY t1 FROM ’filename’ USING DELIMITERS ’,’;

(\h copy is you asking for help on copy, because we always ask for help. growth mindset. 🌱)


πŸ¦‹ Geometry import from PostgreSQL table into GRASS πŸ¦‹

v.in.db creates a new vector (points) map from a database table that's holding coordinates. see here for examples. πŸ“


🌸 PostGIS: PostgreSQL with vector geometry 🌸

PostGIS β€” it adds geographic object support to PostgreSQL. it's PostgreSQL but with ✨spatial feelings✨.

πŸ—ΊοΈ Example: Import from PostGIS πŸ—ΊοΈ

in an existing PostGIS database, create the following table:

CREATE TABLE test
(
 id serial NOT NULL,
 mytime timestamp DEFAULT now(),
 text varchar,
 wkb_geometry geometry,
 CONSTRAINT test_pkey PRIMARY KEY (id)
) WITHOUT OIDS;
# insert value
INSERT INTO test (text, wkb_geometry)
 VALUES (’Name’,geometryFromText(’POLYGON((600000 200000,650000
 200000,650000 250000,600000 250000,600000 200000))’,-1));
# register geometry column
select AddGeometryColumn (’postgis’, ’test’, ’geometry’, -1, ’GEOMETRY’, 2);

then GRASS can import this PostGIS polygon map like so:

v.in.ogr input="PG:host=localhost dbname=postgis user=neteler" layer=test \
         output=test type=boundary,centroid
v.db.select test
v.info -t test

import it, select from it, get the info on it. the full circle moment. 🫢

πŸ”„ Geometry Converters πŸ”„

  • PostGIS with shp2pgsql:
shp2pgsql -D lakespy2 lakespy2 test > lakespy2.sql
  • e00pg: E00 to PostGIS filter, see also v.in.e00.
  • GDAL/OGR ogrinfo and ogr2ogr: GIS vector format converter and library, e.g. ArcInfo or SHAPE to PostGIS.
ogr2ogr -f "PostgreSQL" shapefile ??

πŸ‘€ SEE ALSO πŸ‘€

  • db.connect, db.execute
  • Database management in GRASS GIS
  • Help pages for database modules
  • SQL support in GRASS GIS

πŸ“š REFERENCES πŸ“š

  • PostgreSQL web site
  • pgAdmin graphical user interface
  • GDAL/OGR PostgreSQL driver documentation

πŸ’Ύ SOURCE CODE πŸ’Ύ

Available at: PostgreSQL DATABASE DRIVER source code (history)

Accessed: Saturday Jan 17 05:40:32 2026

Main index | Topics index | Keywords index | Graphical index | Full index


and that's the whole page bestie, you ate that πŸ’…βœ¨

Β© 2003-2025 GRASS Development Team, GRASS GIS 8.4.2 Reference Manual β€” and yes this manual is literally labeled GRASS 8.4.2 at the bottom, grass-pg(1), periodt. πŸ’–πŸŽ€πŸ¦‹πŸŒΈπŸ‘‘

πŸ“¦ where this page comes from