v.db.connect(1grass)

πŸ’– v.db.connect β€” the GRASS manual page, but make it make sense πŸ’–

okay so bestie. this is v.db.connect from the GRASS User's Manual (that's the 1grass one, you found it). it's a vector module, it lives in the GRASS house, and it's about hooking your vector map up to its attribute table. let's get into it 🫢

✨ KEYWORDS ✨

vector, attribute table, database, layer β€” yeah that's the whole vibe, iykyk πŸ’…


🌸 NAME 🌸

v.db.connect β€” the way that it prints and/or sets the DB connection for a vector map to its attribute table. that's it, that's the whole job. iconic behavior honestly πŸ‘‘


πŸ¦‹ SYNOPSIS πŸ¦‹

v.db.connect
v.db.connect --help
v.db.connect [-pgcd] map=name  [driver=name]   [database=name]
[table=name]   [key=name]   [layer=string]   [separator=character]
[format=name]   [--overwrite]  [--help]  [--verbose]  [--quiet]  [--ui]

like. look at her. the flags are all smooshed together as -pgcd, the parameters are all square-bracketed and optional-ish, map=name is the one that's actually required. periodt ✨

πŸŽ€ Flags πŸŽ€

  • -p β€” prints ALL the map connection parameters and then dips out. exit stage left πŸ’…
  • -g β€” prints all the map connection parameters in the legacy format. this one is deprecated, bestie. the order it spits things out in is: layer[/layer name] table key database driver.
⚠️ okay so this flag is deprecated and will be removed in a future release. use format=csv instead. fr fr. don't get attached πŸ’€
  • -c β€” prints the types and names of the table columns for the layer you specified, then exits. very "what's in the box" of it πŸ“¦
  • -d β€” deletes the connection for a certain layer. and like… not the table. just the connection. read it again, it matters 🫢
  • --overwrite β€” lets output files go ahead and overwrite existing files. chaotic permission granted 🫒
  • --help β€” prints the usage summary. the classic.
  • --verbose β€” verbose module output. more words. more ✨
  • --quiet β€” quiet module output. fewer words. shhh 🀫
  • --ui β€” forces the GUI dialog to launch. sometimes you just want buttons, okay? πŸ–±οΈ

🌈 Parameters 🌈

  • map=name β€” required, no skipping this one. name of the vector map. or a data source for direct OGR access. πŸ‘‘
  • driver=name β€” name of the database driver. your options are: dbf, mysql, odbc, ogr, pg, sqlite. default is sqlite. slay
  • database=name β€” name of the database. default: $GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db β€” yeah it's the whole variable soup, that's just how GRASS do πŸ’…
  • table=name β€” name of the attribute table. nothing more, nothing less.
  • key=name β€” name of the key column. it must refer to an integer column, no negotiating. default: cat 🐱
  • layer=string β€” layer number or name. format is layer number[/layer name]. default: 1.
  • separator=character β€” the field separator used when printing output. special characters you can say: pipe, comma, space, tab, newline.
  • format=name β€” the output format, bestie. options: plain, csv, json. and like, here's the breakdown:
    • plain: Human readable text output πŸ“
    • csv: CSV (Comma Separated Values) πŸ“Š
    • json: JSON (JavaScript Object Notation) 🧚

✨ DESCRIPTION ✨

v.db.connect prints or sets the database connection for a vector map. you, the user, the main character, can add or remove the link to the attribute table on whichever layer you want. that's the power πŸ’…


πŸ’œ NOTE πŸ’œ

so the connection info β€” that's driver, database, table, key β€” gets stored for each map, inside this file:

<database>/<project>/<mapset>/vector/<map>/dbln

if the parameters for the database connection are already set with db.connect, then they get taken as default values and you do not have to type them out every single time. thank god 😭

when you're printing the database connection (that's the p or g flag, bestie), the parameter layer is ignored β€” like, all connections get printed to the output, no filter, no cap. πŸ‘€

🚨 ATTENTION. removing a vector map will also delete all tables linked to it. yes really. if you use v.db.connect to link further tables to your map, it is advisable to make a copy of those tables first and then connect the copied tables to the vector map (see also v.overlay). protect your data, bestie, it's giving self-care πŸ₯Ί

πŸ“ EXAMPLE πŸ“

πŸ“Œ note: the default database backend setting is SQLite.

Print all the database connection parameters for a vector map:

v.db.connect -p map=roads

Print the column types and names of the table that's linked to the vector map:

v.db.connect -c map=roads

🌸 Connect vector map to database (DBF driver) 🌸

Connect a vector map to a DBF table, with or without variables.

Using the default DB connection:

v.db.connect map=vectormap table=table

Using a hardcoded path to the DBF directory (not recommended, and I mean it πŸ™ˆ):

v.db.connect map=vectormap table=table \
             database=/home/user/grassdata/spearfish60/PERMANENT/dbf

Using a variable as the DBF directory definition β€” single quotes must be used:

v.db.connect map=vectormap table=table \
             database=’$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/’

Connect vector map layer 2 and key ID to database with variables (note: if needed, single quotes must be used for the database parameter):

v.db.connect map=vectormap table=table layer=2 key=ID

🌸 Connect vector map to database (SQLite driver) 🌸

Very similar to the DBF driver example above, bestie.

db.connect driver=sqlite database=’$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db’
db.tables -p
v.db.connect map=vectormap table=table driver=sqlite \
             database=’$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db’
v.db.connect -p map=vectormap

🌸 Connect vector map to database (MySQL driver) 🌸

# note: connection which requires password
db.connect driver=mysql database="host=dbserver.foo.org,dbname=my_database"
db.login user=joshua [password=xxx]
# ... or enter password interactively.
db.tables -p
# connect external table to layer 2:
v.db.connect map=my_map table=my_mysql_table key=baz layer=2
v.db.connect -p my_map

🌸 Connect vector map to database (PostgreSQL driver) 🌸

# note: connection without password being asked
v.db.connect map=vectormap table=table layer=1 key=oid driver=pg \
             database="host=myserver.itc.it,dbname=mydb,user=name" \
             table=mytable key=id

🌸 Store geometry in GRASS but attributes in PostgreSQL 🌸

This example illustrates a mixed data storage with the possibility to update attributes in an external PostgreSQL database:

# Check current settings for attribute storage:
db.connect -p
# Import table from PostgreSQL to new map
# (NOTE: output map name needs to be different from table name in
#        case that GRASS is connected to PostgreSQL):
v.in.db driver=pg database="host=localhost,dbname=meteo" \
        table=mytable x=lon y=lat key=cat out=mytable
v.db.connect map=mytable -p
# Cancel table connection between map and attribute table:
v.db.connect map=mytable -d
v.db.connect map=mytable -p
# Drop table which was replicated due to import:
db.tables -p
echo "DROP TABLE mytable" | db.execute
db.tables -p
# reconnect map to table in PostgreSQL:
v.db.connect map=mytable driver=pg database="host=localhost,dbname=meteo" \
        table=mytable key=cat
# Now the geometry is stored in GRASS while the attributes are stored
# in PostgreSQL.

An alternative is to create a "view" of only ID, x, y [,z] columns and to use v.in.db on this view, then connect the original table to the geometry. This will be faster if the original table is very large. speed demon behavior πŸƒβ™€οΈπŸ’¨

🌸 Store geometry in GRASS but attributes in PostGIS 🌸

This example illustrated a mixed data storage with possibility top update attributes in external PostGIS database:

# Check current settings for attribute storage:
db.connect -p
# Import table from PostGIS to new map
# (NOTE: output map name needs to be different from table name in
#        case that GRASS is connected to PostGIS):
v.in.db driver=pg database="host=localhost,dbname=meteo" \
        table=mytable x="x(geom)" y="y(geom)" key=cat out=mytable
v.db.connect map=mytable -p
# Cancel table connection between map and attribute table:
v.db.connect map=mytable -d
v.db.connect map=mytable -p
# Drop table which was replicated due to import:
db.tables -p
echo "DROP TABLE mytable" | db.execute
db.tables -p
# reconnect map to table in PostGIS:
v.db.connect map=mytable driver=pg database="host=localhost,dbname=meteo" \
        table=mytable key=cat
# Now the geometry is stored in GRASS while the attributes are stored
# in PostGIS.

πŸ”₯ SEE ALSO πŸ”₯

db.connect, db.copy, db.tables, v.db.addtable, v.db.droptable, v.db.addcolumn, v.db.dropcolumn, v.external, v.in.db, v.overlay

GRASS SQL interface β€” the whole squad, go check them out 🫢


πŸ‘‘ AUTHOR πŸ‘‘

Radim Blazek, ITC-Irst, Trento, Italy. thank you for your service πŸ«‘πŸ’–


⭐ SOURCE CODE ⭐

Available at: v.db.connect source code (history)

Latest change: Saturday Dec 27 00:12:59 2025 in commit: 73206eb2adc9482d1d2ecfef35a169fb97f68609

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

Β© 2003-2026 GRASS Development Team, GRASS 8.5.0 Reference Manual


okay that's the whole page bestie, no notes. this was GRASS 8.5.0 v.db.connect(1grass), and yes the footer said it too. go connect some tables. mwah πŸ’…πŸ’–βœ¨

πŸ“¦ where this page comes from