Problems connecting remotely to PostgreSQL on Heroku from R using RPostgreSQL - r

I'm using the RPostgreSQL 0.4 library (compiled on R 2.15.3) on R 2.15.2 under Windows 7 64-bit to interface to PostgreSQL. This works fine when connecting to my PostgreSQL databases on localhost. I'm trying to get my R code to run with a remote PostgreSQL database on Heroku. I can connect to Heroku's PostgreSQL database from the psql command shell on my machine, and it connects without a problem. I get the message:
psql (9.2.3, server 9.1.9)
WARNING: psql version 9.2, server version 9.1.
Some psql features might not work.
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Clearly, psql uses SSL to connect. When I try to connect using the RPostgreSQL library routine dbConnect(), however, supplying exactly the same credentials using dname=, host=, port=, user=, password=, the connection fails with the complaint:
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect <user>#<hostname> on dbname <dbname>)
Calls: source ... .valueClassTest -> is -> is -> postgresqlNewConnection -> .Call
Execution halted
I know that Heroku insists on an SSL connection if you want to access their database remotely, so it seems likely that the R interface routine dbConnect() isn't trying SSL. Is there something else that I can do to get a remote connection from R to PostgreSQL on Heroku to work?

To get the JDBC URL for your heroku instance:
Get your hostname, username and password using [pg:credentials].
Your jdbc URL is going to be:
jdbc:postgresql://[hostname]/[database]?user=[user]&password=[password]&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
Proceed as you would normally with JDBC.

Apparently there is a way using RJDBC. See:
http://ryepup.unwashedmeme.com/blog/2010/11/17/working-with-r-postgresql-ssl-and-mssql/

Please note that in order to connect to Heroku database with JDBC externally, it is important to set the sslfactory parameter as well. Hope Heroku team goes through it and modifies their documentation.
String dbUri = "jdbc:postgresql://ec2-54-243-202-174.compute-1.amazonaws.com:5432/**xxxxxxx**";
Properties props = new Properties();
props.setProperty("user", "**xxxxx**");
props.setProperty("password", "**xxxxx**");
props.setProperty("ssl", "true");//ssl to be set true
props.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory");// sslfactory to be set as shown above
Connection c=DriverManager.getConnection(dbUri,props);

See answer to related Q at https://stackoverflow.com/a/38942581. The suggestion of using RPostgres (https://github.com/rstats-db/RPostgres) instead of RPostgreSQL resolved this same issue for me.

Related

database protocol 'sqlite' not supported - Failed to initialize zdb connection pool()

I am using libzdb - Database Connection Pool Library with sqlite database. I am getting following exception :
Failed to start connection pool - database protocol 'sqlite' not supported
After ConnectionPool_start() - it goes in static int _fillPool(T p), in that it is getting falied at above statement
Connection_T con = Connection_new(P, &P->error);
My connection url is as follows :
sqlite:///home/ZDB_TESTING/zdb-test/testDb.db
Kindly help me with this problem.
This means that the SQLite library is not compiled into the libzdb library. If installing from a distribution, make sure that you select libzdb built with SQLite. If you built libzdb yourself from source, after you run ./configure make sure the output says, SQLite3: ENABLED. Otherwise you need to install SQLite on your system first.

R- mongolite on OS X Sierra- No suitable servers found

I am trying to follow the "Getting started with MongoDB in R" page to get a database up and running. I have mongoDB installed in my PATH so I am able to run mongod from the terminal and open an instance. Though when I open an instance in the background and try running the following commands in R:
library(mongolite)
m <- mongo(collection = "diamonds") #diamonds is a built in dataset
It throws an error after that last statement saying:
Error: No suitable servers found (`serverSelectionTryOnce` set): [Failed to resolve 'localhost']
How do I enable it to find the connection I have open? Or is it something else? Thanks.
It could be that mongolite is looking in the wrong place for the local server. I solved this same problem for myself by explicitly adding the local host address in the connection call:
m <- mongo(collection = "diamonds", url = "mongodb://127.0.0.1")

Using RODBC in R to connect to Azure SQL

I am using R 3.2.4 and El captain, I wanted to connect to Azure SQL using the RODBC package in R, I did the following to install it
brew install unixodbc
install.packages("RODBC",type="source")
And also
brew install freetds --with-unixodbc
The output of above command:
Warning: unixodbc-2.3.4 already installed
Warning: freetds-0.95.80 already installed
But whenever I try to connect using the following:
library("RODBC")
con = odbcDriverConnect(
'driver = {SQL Server};
Server = xxxxxx;
Database = xxxxx;
User Id= xxxxx;
Password= xxxxx;')
I get the following Error:
[RODBC] ERROR: state IM007, code 59478176, message [iODBC][Driver Manager]No data source or driver specified, dialog prohibited
This is my first post, so please be forgiving. I have got this set up working for a windows environment and an Azure SQL server that use Active Directory.
Plan of attack for this problem:
First try to connect to the Azure database using the SQL server management Studio.
Secondly, try to connect to the Azure database using the odbcad32. If that works you can create a User DSN, which for the example's sake I will call example.
Finally use odbcDriverConnect("DSN=example;") in R
First of all try to login to the Azure database using Sql Server Management Studio. This involves two substeps.
On the target database your user domain and name may be different from the user domain and name on the machine from which you are trying to connect to the target database. Should that be the case then create a credential using the credential manager in windows.
Creating a windows credential to match your identity with that used by the Azure database
Verify the method of authentication from the dialog you get when connecting with SQL server management studio. On my machine I thus saw that the Azure SQL server uses a thing called 'Active Directory Integration' .
Select the method of authentication that applies
Secondly instead of directly testing using RODBC calls it is more convenient to use the ODBC administrator. Run 'odbcad32'. You have to choose the driver.
If you want to use Active Directory for Authentication you need the 'ODBC Driver 13 for SQL Server'. The other drivers, such as 'SQL Server' and the 'Native Client' do not support Active Directory.
Create a user DSN and test it.
The end result should look like this.
If you do not get the desired success you may instead get the following error messages.
If this occurs simply go ahead and install the required software
Go ahead and install the Sign in assistant.
Another error that may pop up is the error saying that you have not installed the Active Directory Authentication Library for SQL Server. Surely this is not true if you have the set up working via SQL server management studio. The problem is that odbcad32 cannot find adalsql.dll. The solution is to add a few registry entries.
Which registry entries to add
Addendum: Below some people note they got it working with the 11.0 driver instead of the 13.0 one. They are perfectly right, but that is because they use user / password authentication. If your server uses active directory integration you do really need the 13.0 or higher versions of the ODBC driver.
So after a lot of trail and error, here is what I have:
1) Ditch RODBC and use RSQLServer
install.packages("RSQLServer")
Create a config file (use sublime) called 'sql.yaml', with the following contents:
SQL_PROD:
server: 11.1.111.11
type: &type sqlserver
port: &port 1433
domain: &domain companyname
user: &user winusername
password: &pass winpassword
useNTLMv2: &ntlm true
SQL_DEV:
server: 11.1.111.15
type: *type
port: *port
domain: *domain
user: *user
password: *pass
useNTLMv2: *ntlm
AW:
server: <yourservername>
type: sqlserver
user: <username>
password: <password>
port: 1433
2) Save this YAML file in the following location(run the following in R: Sys.getenv("HOME")) e.g.: if username is dave its "/Users/dave"
3)
#############
# DBI #
#############
# Note we do not attach the RSQLServer package.
library(DBI)
# Connect to AW server in ~/sql.yaml
aw <- dbConnect(RSQLServer::SQLServer(), "AW", database = 'db')
# RSQLServer only returns tables with type TABLE and VIEW.
dbListTables(aw)
I installed the "odbc" library and setup the connection as described on the RStudio pages. The 11.0 driver worked for me.
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQL Server Native Client 11.0",
Server = "servername",
Database = "databasename",
UID = "username",
#PWD = rstudioapi::askForPassword("Database password"),
PWD = "pwd",
Port = 1433)

Install odbc drive for drill

I followed these steps to config the odbc drive for drill:
Configuring ODBC on Linux
After that I try to test the drill connection according Testing the ODBC Connection Instruction
/usr/bin/iodbctest
iODBC Demonstration program
This program shows an interactive SQL processor
Driver Manager: 03.52.0709.0909
Enter ODBC connect string (? shows list): DSN=Drill;ConnectionType=ZooKeeper;ZKQuorum=192.168.0.253:2181,192.168.0.254:2181;ZKClusterID=drillbits1
Driver: 1.2.0.1000 (MapR Drill ODBC Driver)
SQL>select columns[0] as 'Year',columns[1] as Revenues from 'hdfs.root'.'./user/hdfs/R/DisneyFinancialTest.csv'
1: ERROR [HY000] [MapR][Drill] (1070) Drill fails to execute the query with error [30017]Communication error. End of file
It works well with the connection to sql, but it shows that the drill fails to execute. What does the issue means? Can anyone help me trouble shot this problem?
quit; is interpreted as a SQL command, which is not valid for Drill. If you want to quit isql, then just use quit.
Finally I found out what was the issue, the problem was that my odbc driver version and my drill version is not match. I uninstalled the drill odbc driver 1.2 and installed version 0.8, it works well.
I solved the problem by referencing this link.

How can I connect to a sqlite3 db using pyodbc and unixODBC?

I can't figure out how to connect to a sqlite db using pyodbc. I have a DSN set up and can connect to it via isql without problems.
isql -v TEST
works like a charm, However
import pyodbc
pyodbc.connect('DSN=TEST')
Gives the following error message:
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnectW)')
I also tried a great amount of permutations of the connection strings in the examples at https://code.google.com/p/pyodbc/wiki/GettingStarted to no avail.
pyodbc.dataSources()
Does list the dsn I'm trying to connect to with the value holding the path to libsqlite3odbc.so
I assume I am missing something obvious, but I can't figure out what...
EDIT: Using unixODBC 2.3.0, python 2.7.1, sqliteodbc 0.88 and pyodbc 2.1.8
EDIT: The reason I want to use pyodbc is for portability to Windows and MS Access
Just to check what is being passed to the driver manager, turn on ODBC tracing, in your odbcinst,ini file add
[ODBC]
Trace = Yes
TraceFile = /tmp/sql.log
Then run the app again and see what is being passed to the SQLConnect or SQLDriverConnect call.

Resources