So now I know that it has been asked before many times but I wanted to know what are the main problems that one encounters when using rgdal. To be more specific I have 2 lines of code:

dcV="PG:dname='remote' host=localhost user='postgres' password='pass' port=5432 scehma='public' table='vector2' mode=2"

but I have I get an error that says:

Error in ogrListLayers(dsn = dsn) : Cannot open data source

and I have no idea what is causing it because I loaded a raster layer the same way but instead of "vector2" I wrote "raster2".And yes both of them exist and can be fetched from QGIS P.S I am working with Linux and I am calling the Rscript from the terminal

EDIT: When I typed drv = ogrDrivers();drv[grepl("PostGIS",drv$long_name),] I got the following output:

enter image description here

  • What's the state of your postGIS driver in rgdal? Run drv = ogrDrivers();drv[grepl("PostGIS",drv$long_name),] and add the output to your Q. Also, what was "raster2"? A PostGIS Raster? What sort of R object did you get back? With readOGR? Or SpatialPixelsDataFrame? Include that and the output in your Q too for completeness. – Spacedman Aug 7 '17 at 15:36
  • @Spacedman Yeah, I used raster2psql to input that in my database and actually got back my raster. I can upload the syntax of that if you want, but the point is that I can't fetch the vector data using readOGR and now the output (from the line that you provided) is this: name long_name write copy isVector 58 PostgreSQL PostgreSQL/PostGIS TRUE FALSE TRUE – George Nostradamos Aug 7 '17 at 15:53

Yeah so the solution was pretty simple, the only thing I had to change was

dcV="PG:dname='remote' host=localhost user='postgres' password='pass' port=5432 scehma='public' table='vector2' mode=2"


dcV="PG:dname='remote' host=localhost user='postgres' password='pass' port=5432

and then I added 1 line and 1 parameter. Where the line was:ogrListLayers(dcV) so I can see what was the name of my layer and to check if my PostGIS was on.

Now the parameter was in this line: poly=readOGR(dcV) where I changed it to poly=readOGR(dsn=dcV, layer="vector2") adding the name of my vector

P.S I you want to add a raster layer to your PostGIS database, the general command for Linux using the pipe method is:

raster2pgsql -I -C -s <SPATIAL_REF_ID> <PATH_TO_RASTER FILE> <SCHEMA>.<DBTABLE> |psql -h <h as in host> -U <U as in username> -p <p as in port> -d <d as in database>
  • I just noticed your typo for "schema": scehma='public' - was that also killing it? – Spacedman Aug 8 '17 at 6:46
  • No, that was my mistake as I was typing it here – George Nostradamos Aug 8 '17 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.