7

I'm trying to connect to a remote Postgres database with ssl = verify ca mode. My problem seems to be similar to Connect to Redshift via SSL using R and Connect to Postgres via SSL using R, but they do not work properly. The error is always

Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect (null)@datadb1 on dbname "(null)"

My code is something like this

library("RPostgreSQL")
host = 'datadb1'
dbname = 'test'
port = 5432
password = pw
username = 'pep'

pg_dsn = paste0(
  'dbname=', dbname, ' ',
  'sslrootcert=', "C://root-ca.crt", ' ',
  "sslkey=C://pep.key", " ",
  "sslcert=C://pep.crt", 
  'sslmode=verify-ca'
)
dbConnect(RPostgreSQL::PostgreSQL(), dbname=pg_dsn, host=host, 
          port=port, password=password, user=username)

It's not a general database problem though, because I'm able to connect to the db using Python. Update: I had made a mistake in specifying the path; the error is actually this:

Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect pep@datadb1 on dbname "test") 
Community
  • 1
  • 1
Alex
  • 978
  • 1
  • 9
  • 22

1 Answers1

6

According to the error message, the problem is that you're passing empty values for the username and database name. That suggests your actual code doesn't match what you've entered here. I would write a 10-line Rscript program that just connects and grabs a bit of data, like this:

#!/usr/bin/Rscript

library("RPostgreSQL")
host = '192.168.36.2'
dbname = 'test'
port = 5432
password = 'secret'
username = 'pep'

pg_dsn = paste(
                'dbname=', dbname, ' ',
                'sslrootcert=', 'rootCA.pem', ' ',
                'sslkey=pem.key', ' ',
                'sslcert=pem.crt', ' ',
                'sslmode=verify-ca',
                sep=""
                )

conn <- dbConnect(RPostgreSQL::PostgreSQL(), dbname=pg_dsn, host=host,
          port=port, password=password, user=username)
rs <- dbSendQuery(conn, statement="SELECT COUNT(*) FROM users")
data <- fetch(rs, n=1)
dim(data)

So I don't think this is related to SSL certs at all, but the fact that your variables aren't being set the way you think they are.

EDIT: I created my own CA and used it to sign a server cert and a client cert. I put Postgres 9.3 on a fresh VM and have connections working, with certs required on both sides. I can connect with both psql and R. So I'm afraid I can't reproduce your problem. But a few things look suspicious in your code:

  • You only need one forward slash in your paths, not two. (If you were using backslashes you'd need two.)
  • You need a space before sslmode, like this:

    'sslcert=pem.crt', ' ',
    

    not this:

    'sslcert=pem.crt',
    

Do either of those changes fix your problem?

Paul A Jungwirth
  • 23,504
  • 14
  • 74
  • 93
  • If I just drop all the SSL certs and run the above code, this is the error: `Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect pep@datadb1 on dbname "test" )`. I think I read somewhere that RPostgreSQL doesn't support SSL connections, so I might have to switch the library completely – Alex Mar 14 '16 at 17:55
  • Okay, edited the answer to try a couple more things. – Paul A Jungwirth Mar 14 '16 at 19:34
  • Thanks for going through the trouble of setting it up, and for your patience. I did the two points you mentioned, but still is giving the same error.I asked a couple of people to check it in case I'm missing sth, but no. I'm thinking of a way to make a reproducible example, but I can't think of any at the moment – Alex Mar 15 '16 at 21:12
  • 1
    I still think `"(null)"` in your error message is a strong signal your variables simply aren't being set correctly. I've updated my test program to include SSL cert stuff. If you change the filenames to match your own, does that give you the same error? – Paul A Jungwirth Mar 15 '16 at 22:17
  • You were right about the names not being set correctly. I had them in folder `c:/db cred`, and when I moved them to just `c:/users/pep` the `null` problem was solved. My bad, sorry. But the error is now: `Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect pep@datadb1 on dbname test`) – Alex Mar 16 '16 at 20:39
  • `Error in postgresqlNewConnection(drv, ...) : RS-DBI driver: (could not connect pep@datadb1 on dbname "test")` – Alex Mar 16 '16 at 20:49
  • Since you were able to connect to database, could it be because I'm on Windows? – Alex Mar 17 '16 at 16:29