grass-pg(1grass)

🐘✨ PostgreSQL DATABASE DRIVER ✨🐘

okay bestie, welcome to grass-pg(1grass) β€” straight out of the GRASS User's Manual πŸ’– this is the page that tells you how to hook GRASS up to Postgres, and honestly? it's giving iconic.

So what even is this. The PostgreSQL database driver enables GRASS to store vector attributes in a PostgreSQL server. that's the whole vibe. attributes? in postgres? yes. slay. πŸ’…


🌸 Creating a PostgreSQL database 🌸

A new database is created with createdb. that's it, that's the tea. go read the PostgreSQL manual for the deets, i'm not gonna hold your hand through that one 🫢


πŸ”Œ Connecting GRASS to PostgreSQL πŸ”Œ

Here's your lil starter pack, copy it and go:

# 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

look at that. beautiful. no notes. 😭

πŸ” Username and password πŸ”

straight from the PostgreSQL manual, quote unquote:

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

that's the format fr fr. memorize it or don't, it's your life πŸ’€


πŸ“ Supported SQL commands πŸ“

All SQL commands supported by PostgreSQL. iconic, generous, we love her.

⚠️ but NOT possible to use C-like escapes (with backslash like \n etc) within the SQL syntax. so don't try it. she said no. 🚫

βž— Operators available in conditions βž—

All SQL operators supported by PostgreSQL. that's right, all of them. go feral. πŸ”₯


πŸ†” Adding an unique ID column πŸ†”

okay this one matters β€” the Import vector module require an unique ID column, and you can generate one like this in a PostgreSQL table:

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"

four commands, one unique ID, zero drama ✨


πŸ“₯ Attribute import into PostgreSQL πŸ“₯

CSV import into PostgreSQL, as follows:

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

easy peasy bestie πŸ“


πŸ—ΊοΈ Geometry import from PostgreSQL table into GRASS πŸ—ΊοΈ

v.in.db creates a new vector (points) map from a database table containing coordinates. See here for examples. yes, points. spatial. main character energy, honestly πŸ‘‘


🌈 PostGIS: PostgreSQL with vector geometry 🌈

PostGIS: adds geographic object support to PostgreSQL. so postgres but with geography. the glow up was real πŸ¦‹

🌟 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);

GRASS can import this PostGIS polygon map as follows:

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

look at her go. polygon in, polygon out. periodt πŸ’…

πŸ”„ 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 ??

(yes that ?? is in there, no i do not know, don't ask me, i'm just the messenger 😭)


πŸ‘€ SEE ALSO πŸ‘€

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

πŸ“š REFERENCES πŸ“š

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

πŸ’Ύ SOURCE CODE πŸ’Ύ

Available at: PostgreSQL DATABASE DRIVER source code (history)

Accessed: Tuesday Sep 08 11:54:02 2026

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


and that's the whole page bestie 🫢 tysm for reading, you absolutely ate that. this came to you courtesy of the 🌈 GRASS Development Team 🌈, Β© 2003-2026, from the GRASS 8.5.0 Reference Manual, and yes this whole masterpiece lives under the name grass-pg(1grass) in GRASS 8.5.0. mwah πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from