简体   繁体   中英

rgdal error when connecting to postgres

I'm working with a Mac Os 10.9.2 and a R version 3.0.2. I used dbDriver() and dbConnect() to initiate the connection to my database. Next, I tried to connect to my postgres database using

c = readOGR("PG:dbname=OB", layer="geo.countries")

This does not work, and always returns a "Cannot open file" error. I understood from https://stat.ethz.ch/pipermail/r-sig-geo/2010-January/007519.html that the reason for this is the absence of a driver for PostgreSQL. As can be seen by using the command ogrDrivers()

Does anybody can help me on how to install the driver? Or how I can make this work? Any help is much appreciated!

Thanks!

In the absence of the right driver, gdal/ogr usually throws and error like

Unable to find driver PostgreSQL

First, make sure that the database and layer exist. If it's true that the driver for Postgres isn't installed, you'll have to re-install gdal. Using homebrew:

brew uninstall gdal
brew install gdal --with-postgresql

See also this question .

If you are really sure that gdal is properly installed, make sure that your dns is fully specified (it has more parameters than dbname)

dsn="PG:dbname=DB host=HOST user=USER password=PSSWD port=5432"

Moreover, if your table contains more than one spatial columns (layers), you have to specify the one you want:

layer = "DB.TABLE(YOUR_SPATIAL_COLUMN)"

Took me a while to find out. But it was obvious after calling function

ogrListLayers()

It works for me. Just one issue, if you have some raster column in you table, it will not be excluded (as all other layers/spatial columns). Instead, it will be loaded into spatialobject@data dataframe as text column. Quite annoying in case of big rasters. I have posted question for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM