## NAME

v.db.connect - Prints/sets DB connection for a vector map to attribute table

## KEYWORDS

vector, database, attribute table

## SYNOPSIS

v.db.connect
v.db.connect help
v.db.connect [-pgcod] map=name [driver=string] [database=string] [table=string] [key=string] [layer=integer]

### Flags:

-p
  Print current map connection parameters and exit
-g
  Print current map connection parameters and exit (shell script style) format: layer[/layer name] table key database driver
-c
  Print types/names of table columns for specified layer and exit
-o
  Overwrite connection parameter for certain layer
-d
  Delete connection for certain layer (not the table)

### Parameters:

map=name
  Name of input vector map
driver=string
  Driver name
  Options: dbf,pg,ogr
database=string
  Database name
table=string
  Table name
key=string
  Key name (must refer to an integer column)
  Default: cat
layer=integer
  Layer number
  Default: 1

## DESCRIPTION

v.db.connect prints or sets database connection for a vector map.

## NOTE

If parameters for database connection are already set with db.connect, they are taken as default values and do not need to be spcified each time.

Attention: Removing a vector map will also delete all tables linked to it! If you use v.db.connect to link further tables to your map, it is advisable to make a copy from those tables first and connect the copied tables to the vector map (see also v.overlay).

## EXAMPLE

Print database connection

    v.db.connect -p map=vectormap

Print column types and names of table linked to vector map

    v.db.connect -c map=vectormap

### Connect vector map to database (DBF driver)

Connect vector map to database with variables
Using default DB connection:

    v.db.connect map=vectormap table=table

Using hardcoded path to DBF directory:

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

Using variable as 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 (PostgreSQL driver)

    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 illustrated a mixed data storage with possibility top update attributes in 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.

### 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.external, v.in.db, v.overlay,

## AUTHOR

Radim Blazek, ITC-Irst, Trento, Italy

Last changed: $Date: 2006/03/23 17:57:40 $

Main index - vector index - Full index