# PostgreSQL

This driver implements support for access to spatial tables in PostgreSQL extended with the PostGIS spatial data support. Some support exists in the driver for use with PostgreSQL without PostGIS but this has not been recently tested and is likely decayed.

When opening a database, it's name should be specified in the form "PG:dbname=<databasename>". It's also possible to omit the database name and connet to default one, with the same name as the user name. We use PQconnectdb() to make the connection, so any other options and defaults that would apply to it, apply to the name here. The PG: prefix is used to mark the name as a postgres connection string.

If the geometry_columns table exists, then all listed tables and named views will be treated as OGR layers. Otherwise all regular user tables will be treated as layers.

Regular (non-spatial) tables can be accessed, and will return features with attributes, but not geometry. If the table has a "wkb_geometry" field, it will be treated as a spatial table. The type of the field is inspected to determine how to read it. It can be a PostGIS geometry field, which is assumed to come back in OGC WKT, or type BYTEA or OID in which case it is used as a source of OGC WKB geometry.

If there is an "ogc_fid" field, it will be used to set the feature id of the features, and not treated as a regular field.

The layer name may be of the form "schema.table". The schema must exist, and the user needs to have write permissions for the target and the public schema.

The PostgreSQL driver passes SQL statements directly to PostgreSQL by default, rather than evaluating them internally when using the ExecuteSQL() call on the OGRDataSource, or the -sql command option to ogr2ogr. Attribute query expressions are also passed through to PostgreSQL. It's also possible to request the driver to handle SQL commands with OGR SQL engine, by passing "OGRSQL" string to the ExecuteSQL() method, as name of the SQL dialect.

The PostgreSQL driver in OGR supports the OGRDataSource::StartTrasaction(), OGRDataSource::CommitTransaction() and OGRDataSource::RollbackTransaction() calls in the normal SQL sense.

## Caveats

- The type recognition logic is currently somewhat impoverished. The types "INT*" and "NUMERIC(width,0)" are mapped to integer, the types "FLOAT*" and "NUMERIC(width,precision>0)" are mapped to real, date, time, timestamp and datetime are handled as date types and all other types are just treated as strings.

- A sequence object called <tablename>_ogc_fid_seq is created for new tables (layer)

- Sequential reading is done within a single transaction. Any attempts to write to a layer within a sequential read will likely result in a "BEGIN" while already within a transaction type of error message.

## Creation Issues

The PostgreSQL driver does not support creation of new datasets (a database within PostgreSQL), but it does allow creation of new layers within an existing database.

As mentioned above the type system is impoverished, and many OGR types are not appropriate mapped into PostgreSQL.

If the database has PostGIS types loaded (ie. the geometry type) newly created layers will be created with the PostGIS Geometry type. Otherwise they will use OID. By default it is assumed that text being sent to Postgres is in the UTF-8 encoding. This is fine for plain ASCII, but can result in errors for extended characters (ASCII 155+ for instance). Whle OGR provides no direct control over this, you can set the PGCLIENTENCODING environment variable to indicate the format being provided. For instance, if you text is LATIN1 you could set the environment variable to LATIN1 before using OGR and input would be assumed to be LATIN1 instead of UTF-8.

### Dataset Creation Options

None

### Layer Creation Options

- GEOM_TYPE: The GEOM_TYPE layer creation option can be set to one of "Geometry", "BYTEA" or "OID" to force the type of geometry used for a table.

- OVERWRITE: This may be "YES" to force an existing layer of the desired name to be destroyed before creating the requested layer.

- LAUNDER: This may be "YES" to force new fields created on this layer to have their field names "laundered" into a form more compatible with PostgreSQL. This converts to lower case and converts some special characters like "-" and "#" to "_". If "NO" exact names are preserved. The default value is "YES". If enabled the table (layer) name will also be laundered.

- PRECISION: This may be "YES" to force new fields created on this layer to try and represent the width and precision information, if available using NUMERIC(width,precision) or CHAR(width) types. If "NO" then the types FLOAT8, INTEGER and VARCHAR will be used instead. The default is "YES".

- DIM={2,3}: Control the dimension of the layer. Defaults to 3. Important to set to 2 for 2D layers with PostGIS 1.0+ as it has constraints on the geometry dimension during loading.

- GEOMETRY_NAME: Set name of geometry column in new table. If omitted it defaults to wkb_geometry.

- SCHEMA: Set name of schema for new table. Using the same layer name in different schemas ist supported, but not in the public schema and others.

### Environment variables

- PG_USE_COPY: This may be "YES" for using COPY for inserting data to Postgresql. COPY is less robust than INSERT, but signficantly faster.

- PGSQL_OGR_FID: Set name of primary key instead of 'ogc_fid'.

### Example

Simple translation of a shapefile into PostgreSQL. The table 'abc' will be created with the features from abc.shp and attributes from abc.dbf. The database instance (warmerda) must already exist, and the table abc must not already exist.

    % ogr2ogr -f PostgreSQL PG:dbname=warmerda abc.shp

This second example loads a political boundaries layer from VPF (via the OGDI driver), and renames the layer from the cryptic OGDI layer name to something more sensible. If an existing table of the desired name exists it is overwritten.

    % ogr2ogr -f PostgreSQL PG:dbname=warmerda \
            gltp:/vrf/usr4/mpp1/v0eur/vmaplv0/eurnasia \
            -lco OVERWRITE=yes -nln polbndl_bnd 'polbndl@bnd(*)_line'

In this example we merge tiger line data from two different directories of tiger files into one table. Note that the second invocation uses -append and no OVERWRITE=yes.

    % ogr2ogr -f PostgreSQL PG:dbname=warmerda tiger_michigan \
         -lco OVERWRITE=yes CompleteChain
    % ogr2ogr -update -append -f PostgreSQL PG:dbname=warmerda tiger_ohio \
         CompleteChain

This example shows using ogrinfo to evaluate an SQL query statement within PostgreSQL. More sophisticated PostGIS specific queries may also be used via the -sql commandline switch to ogrinfo.

    ogrinfo -ro PG:dbname=warmerda -sql "SELECT pop_1994 from canada where province_name = 'Alberta'"

This example shows using ogrinfo to list PostgreSQL/PostGIS layers on a different host.

    ogrinfo -ro PG:'host=myserver.velocet.ca user=postgres dbname=warmerda'

### See Also

- PostgreSQL Home Page

- PostGIS

- PostGIS / OGR Wiki Examples Page