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!

Answers


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.


Need Your Help

Deploying to ec2 (preferably via git)

git heroku deployment amazon-web-services amazon-ec2

I've always used heroku free version, that's why deploying has always been easy (via git push to my heroku's repo and that was it). Now I'm trying amazon ec2. Can I do the same for ec2? If not, what

multiple validation error for a textbox using jquery

javascript jquery

I want to print specific emty error message as well as number error message for the age field while during the button click. for my code only the last error message is diplaying.