Connecting to SQL Server with pyodbc, FreeTDS, and Kerberos authentication on macOS - macos

I never ask questions to forums, as I can generally find the answer somewhere on the interweb.
However, in this instance I cannot.
Summary: I can connect to and query the database with kerberos authentication via Azure Data Studio and tsql with FreeTDS. I cannot connect to the pyodbc. I've tried tens of different configurations with no success.
My ultimate goal is to connect to the MSSQL server DB with python.
Thank you for any input.
Background
local machine macOS 10.15.4
Connected to VPN required for kerberos authentication
Have successfully queried DB from Azure Data Studio
database is Microsoft SQL Server 2016
FreeTDS
tsql -S -U 'directory\username' -> Works, can query DB
isql
isql dsn_name 'directory\username' 'password'
error DIAG [42000] [FreeTDS][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
isql dsn_name 'directory\username'
error: DIAG [42000] [FreeTDS][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Windows authentication.
pyodbc
cnxn = pyodbc.connect('DSN=dsn_name;Trusted_Connection=yes')
error:
pyodbc.ProgrammingError: ('42000', '[42000] [FreeTDS][SQL Server]Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. (18452) (SQLDriverConnect)')
cnxn = pyodbc.connect('DSN=dsn_name;UID=directory\username;PWD="password"')
error:
DIAG [01000] [FreeTDS][SQL Server]Adaptive Server connection failed
pyodbc.OperationalError: ('08001', '[08001] [FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
Configuration
krb5.conf
libdefaults
default_realm = domain
[realms]
domain_same_as_default = {
kdc = kdc_address
}
odbc.ini
[dsn_name]
Description = MSSQL Server
Driver = FreeTDS
Servername = server_name
odbcinst.ini
[FreeTDS]
Description=FreeTDS Driver for Linux & MSSQL
Driver=/usr/local/lib/libtdsodbc.so
Setup=/usr/local/lib/libtdsodbc.so
UsageCount=1
[ODBC]
Trace=Yes
TraceFile=/dev/stdout
freetds.conf
[server_name]
host = ip_address
port = port_num
database = db_name
REALM = DOMAIN

I've avoided using DSNs with pyodbc, as I prefer to have all my configuration in one spot. Here's an example connection string I use with a domain.
con = pyodbc.connect(
r"DRIVER={FreeTDS};"
r'SERVER=mssql.mydomain.com;'
r"PORT=1433;"
r"DATABASE=my_db;"
f"UID=MYDOMAIN\\my_username;"
f"PWD=my_password;"
r"TDS_Version=7.3;"
r"Encrypt=yes;"
r"Trusted_Connection=yes;"
)
Give that a whirl? The two backslashes (\\) are needed for escaping if using Windows domain auth, that is not a typo. The key thing you may be missing is TDS_Version. You can read more about TDS Versions here: https://www.freetds.org/userguide/ChoosingTdsProtocol.html
Good luck!

Related

Oracle 11g XE - Can't login and database is down

Just installed Oracle 11g XE (Windows), but can't connect in SQL Developer or run any command in CLI.
When try to connect in CLI using SYS or SYSTEM with password defined during the install, get the following error:
ORA-12638: credential retrieval failed
When I tray to connect via SQL Developer (tried via SID and Service Name, hostname=localhost, port=1521, SID=xe), get this error:
Status : Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in
connect descriptor
Already tried change SQLNET.AUTHENTICATION_SERVICES = (NTS) to (NONE) in sqlnet.ora file. When I do this, get the following error when try to connect with the SYS or SYSTEM user and password:
ORA-01034: ORACLE not available
ORA-27101: shared memory realm does not exist
Already tried every solution in Google results, like reinstall, stop and start services... Mostly ask for first connect in CLI to make some changes, but even this I can't accomplish.
Any suggestions on how connect to the database?
Found the solution:
I was installing in my company Windows user domain.
First uninstall any instance of Oracle XE 11g.
Switch to a local administrator Windows user and install Oracle XE 11g.
Sign off and back to company Windows user.
Edit the file C:\oraclexe\app\oracle\product\11.2.0\server\network\ADMIN\sqlnet.ora (or equivalent to your installation path). Change SQLNET.AUTHENTICATION_SERVICES = (TNS) to SQLNET.AUTHENTICATION_SERVICES = (NONE).
This change will allow you connect to database from you company Windows user.
As seen here: Error ORA-12638 in Oracle Database 11g
Try the following: Open command window, cd to Oracle bin directory, enter sqlplus /nolog. If you get a prompt enter connect / as sysdba. If you get connected then you can try "startup".
- -
Did you try to start the Oracle RDBMS service via Services (services.msc)?
Do you use an spfile? Check the registry for ORA_SID_NAME_PFILE that is different from default location of $ORACLE_HOME/dbs. Also check the default location. Use only the default location or the registry entry removing either the registry entry or the file in the default location depending on which you want to use.

How to connect to Pervasive SQL server in Ruby Application?

I am trying to connect to a Pervasive Sql Server which is running on Windows machine from my Ruby application which is on Ubuntu Machine. Can Someone please help me on same. Thanks in advance
Below is my configuration on linux machine
/etc/freetds/freetds.conf
[pserver]
host = XXX.XXX.XX.XXX
port = 1583
tds version = 8.0
Under /etc/odbcinst.ini, I have saved the driver info like below
[freetds]
Description=freetds Driver
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
Trace=Yes
TraceFile=/tmp/freetds.log
ForceTrave=Yes
UsageCount=10
fileusage=1
dontdlclose=1
TDS_Version = 8.0
In /etc/odbc.ini, client and database details
[pclient]
Description = Pervasive SQL Server
Driver = freetds
Trace = Yes
Servername = pserver
Port = 1583
Database = "MyDatabasename"
TDS_Version = 8.0
tsql -S pserver -U db_username -P db_password -d MyDatabasename
above cmd gives error saying
Error 20009 (severity 9):
Unable to connect: Adaptive Server is unavailable or does not exist
OS error 111, "Connection refused"
There was a problem connecting to the server
iodbctest ["DSN=pclient;UID=db_username;PWD=db_password"]
this results in
iODBC Demonstration program
This program shows an interactive SQL processor
Driver Manager: 03.52.0812.0326
(iodbctest:6672): Gtk-WARNING **: 20:40:38.693: cannot open display:
Not able to understand the above result and how to handle this. Please help
As far as I know, the only way to access Pervasive PSQL from Ruby would be through ODBC. You need to use the Pervasive Client ODBC driver. Don't use the FreeTDS driver. I've never heard of using the FreeTDS driver to connect to Pervasive PSQL. In fact, this question mentions that FreeTDS doesn't work to Pervasive PSQL. Pervasive PSQL and Microsoft SQL Server are not the same product and the client for one cannot connect to the server of the other.
The short answer would be to install the Pervasive PSQL client on the Ubuntu machine, create the ODBC Datasource name pointing to the database on the Windows machine and then use ODBC from within Ruby to access the data.
You'll want to be on at least Pervasive PSQL v11.30, preferably Actian PSQL v13 (current version as of December 2018). You'll need to download and install the client appropriate for the application. If the Ruby application is 32 bit, you need the 32 bit ODBC driver. If the application is 64 bit, you'll need the 64 bit driver. The 'bitness' of the OS is not as important. You need to use the same PSQL client version as server version. You cannot use the v13 client with a v11 server.

ORA-12170: TNS:Connect timeout occurred on CentOS 7

I am configuring a server in CentOS 7 to connect to an Oracle database within the same network. IP ORACLE = 192.167.10.100 IP SERVER = 192.167.15.123
I have installed the Instant Client Package: Basic, SDK and SQL * Plus. Download link
Then set the environment variables:
ORACLE_HOME=/usr/lib/oracle/12.2/client64
PATH=$PATH:$ORACLE_HOME/bin
LD_LIBRARY_PATH=$ORACLE_HOME/lib
TNS_ADMIN=$ORACLE_HOME/network/admin
When entering with the SQL * Plus through the implicit connection string: With the format.
sqlplus username/password#host:port/sid
sqlplus testuser/orcltiger#192.167.10.100:1521/testdb
I get the error: ORA-12170: TNS: the connection timeout occurred
I have been able to connect with the same credentials from a Windows PC with Oracle SQL Developer but from CentOS I get an error:
Someone to help me find the solution, please.

Connecting to Oracle 12 on remote server using Python 2.7.12 results into ORA-12170

Both databases are on remote server and I can get connected to and query on them using TOAD.
When connecting to database configured with OraClient11g_Home1 from Python on my desktop the connection is established successfully. However, trying to connect to database which is using OraClient12Home1 results into ORA-12170 error,i.e. TNS: Connect timeout occurred. Below are configurations.
Edited to contain more information:
I connect to the database using a remote desktop connection. The code is written to automate part of my testing activities by querying two databases and checking whether a single command has been successful on multiple systems(e.g. Ericsson and Huawei)
Output of one query is the input to another one (I can get output from the 11g DB and have previously wrote scripts for it, but this is the first time we're getting connected to the DB on Ora12 using python. I can access both DBs using TOAD on the remote desktop or connect and query 11g DB using python on my desktop but Ora12 throws time out for the same code.
the connection part of the code and how they are queried is as below:
#Get chrono number, action code and status from provisioning table
ip = '********'
port = *****
service_name = '*****'
dsn = cx_Oracle.makedsn(ip, port, service_name)
connection = cx_Oracle.connect("********","********",dsn)
cursor = connection.cursor()
totalChronoList = list()
myQuery=list()
inputData = list()
myQuery = ("select CHRONO_NUM_N, ACTION_CODE_V, STATUS_V from gsm_subs_provisioning where ACTION_DT_DT > SYSDATE - 2 order by ACTION_DT_DT desc")
cursor.execute(myQuery)
inputData.append(cursor.fetchall())
The configurations are as below:
OraClient11g_home1 (11.2.0.1)
ORACLE_HOME:C:\Oracle\product\11.2.0\client_1
ORACLE_HOME_NAME:OraClient11g_home1
ORACLE_HOME_KEY:HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_OraClient11g_home1
ORACLE_SID:
NLS_LANG:AMERICAN_AMERICA.WE8MSWIN1252
SQLPATH:C:\Oracle\product\11.2.0\client_1\dbs
LOCAL:
Client DLL:C:\Oracle\product\11.2.0\client_1\oci.dll
TNSNames.ora:C:\Oracle\product\11.2.0\client_1\Network\Admin\tnsnames.ora
SQLNet.ora:C:\Oracle\product\11.2.0\client_1\Network\Admin\sqlnet.ora
LDAP.ora:C:\Oracle\product\11.2.0\client_1\Network\Admin\ldap.ora
Login.sql:
GLogin.sql:
In system PATH:Yes
Home is valid:Yes
OraClient12Home1 (12.1.0.2)
ORACLE_HOME:E:\app\client\Oracle\product\12.1.0\client_1
ORACLE_HOME_NAME:OraClient12Home1
ORACLE_HOME_KEY:HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_OraClient12Home1
ORACLE_SID:
NLS_LANG:AMERICAN_AMERICA.WE8MSWIN1252
SQLPATH:E:\app\client\Oracle\product\12.1.0\client_1\dbs
LOCAL:
Client DLL:E:\app\client\Oracle\product\12.1.0\client_1\bin\oci.dll
TNSNames.ora:
SQLNet.ora:E:\app\client\Oracle\product\12.1.0\client_1\Network\Admin\sqlnet.ora
LDAP.ora:
Login.sql:
GLogin.sql:E:\app\client\Oracle\product\12.1.0\client_1\sqlplus\admin\glogin.sql
In system PATH:Yes
Home is valid:Yes
ORA-12170: TNS:Connect timeout occurred means you can't access the host and/or port of the DB. I bet in your case it is some restriction on firewalls (most reason, but may be others). First of all try to check is the port accessible. Easiest way - run powershell statement:
Test-NetConnection <host-or-ip> -port <port>
Then go with findings to sysadmin/dba.
Update: As you connect to DB using easy access method (ip, port, service name), you don’t have to care about tnsnames.ora.
In your 12c client we can see that no tnsnames.ora file is found.
Copy this file from the 11g client directory.

Heroku Database Connection Properties

I'm trying to perform a relatively trivial task: I want to connect to a Heroku database. I have created the database and have been issued credentials from the Heroku site. However, when I try to connect to this database using anything besides the terminal 'heroku' command line client, I get fatal errors or cannot connect errors.
The two tools that I tried to connect with outside of the Heroku terminal application are: Navicat and IntelliJ.
The error that I receive in Navicat when trying to connect to the database is:
could not connect to server: Host is down
Is the server running on host "ec2-107-21-112-215.compute-1.amazonaws.com" and accepting
TCP/IP connections on port 5432?
My connection settings are as follows:
Connection Name Heroku Dev Test
Host Name/IP Address ec2-107-21-112-215.compute-1.amazonaws.com
Port 5432
Navicat doesn't even seem to be making an attempt to connect to that hostname.
When I try to connect with IntelliJ, using the full credentials, I get the following error:
java.sql.SQLException: FATAL: no pg_hba.conf entry for host "75.168.4.146", user "rphbqggxeokuxl", database "dc008iqk0rq4j5", SSL off
Again, I'm using the credentials that the Heroku application provides me with when accessing my database on their website.
Has anyone ran into this Heroku connection issue before?
I also had the issue with the FATAL: no pg_hba.conf entry for host error message.
I solved the connection issue to my Heroku Postgres database by adding the following to my JDBC string: &ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory.
Example
jdbc:postgresql://host:port/database?user=username&password=secret&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
You will need the SSL option only if SSL is enabled for your Postgres database (which is the default).
Hint
If you want to check your database connection properties, then just run the following command with the Heroku Toolbelt: heroku pg:info --app your-heroko-appname (make sure that you have Postgres insalled to run this command in your terminal)
The pg:info command will also tell you that sslmode is set to require.
To test the database connection I recommend SQL Power Architect as it is the tool which I was using to check my solution.
Heroku provides this information for connecting from external sources:
https://devcenter.heroku.com/articles/heroku-postgresql#external-connections-ingress
The second error message indicates PostgreSQL is not configured to accept the connection you're trying to make. Given the information Heroku provides, a good guess is that you're not connecting with SSL. Try to enable that on your connection.
Here are instructions for using SSL with Navicat: http://mirror.navicat.com/manual/online_manual/en/navicat/rv_manual/ClientCert.html.
This may be helpful in configuring Intellij to use SSL: https://devcenter.heroku.com/articles/connecting-to-relational-databases-on-heroku-with-java#connecting-to-a-database-remotely.
IntelliJ -> Datasources and Drivers
After you've configured the host, database and user details under the General tab switch to the Advanced tab and ensure that you've added the following:
ssl = true
sslfactory = org.postgresql.ssl.NonValidatingFactory
sslmode = require
You may consider setting a ENVIRONMENT CONFIG VARIABLE 'PGSSLMODE' to 'require' via Heroku's web interface or CLI.
Case: Postgres dB set up as a Heroku add-on and attached to app on a Heroku Dyno.
Heroku's instructions unfortunately leave out any mention of how to activate SSL, even though it is required for any dB tier starting with Standard-0 by default.
Follow all of the pg-copy or pg-upgrade steps (preferred approach depends on your version of Postgres) in Heroku instructions; however, before decommissioning the old database (if relevant) -- add the PGSSLMODE environment variable.
The instructions sufficiently cover how to promote the new database (and, consequently set the DATABASE_URL), so no changes/modifications to them should be required.
Wanted to help others who might run into this.
If you're supplying the Username and Password in seperate fields rather than on the command line, you need to use a ? between the database name and ssl=true and discard the first &
jdbc:postgresql://host:port/database?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
That's the command line that finally allowed me to connect to a PostgreSQL database using SQL Power Architect
For those who might be using Spring Boot and having the configuration provided through the DATABASE_URL environment property (not system property), the suffix can be added to the property:
?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory
and passed through with a slight modification to the config bean:
#Bean
public BasicDataSource dataSource() throws URISyntaxException {
URI dbUri = new URI(System.getenv("DATABASE_URL"));
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
StringBuilder dbUrl = new StringBuilder(128);
dbUrl.append("jdbc:postgresql://")
.append(dbUri.getHost()).append(":")
.append(dbUri.getPort())
.append(dbUri.getPath());
String query = dbUri.getQuery();
if (null != query && !query.isEmpty()) {
dbUrl.append("?").append(query);
}
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setUrl(dbUrl.toString());
basicDataSource.setUsername(username);
basicDataSource.setPassword(password);
return basicDataSource;
}
I'm using node.js and was trying to run my knex migrations for my Heroku app. I tried appending ?sslmode=require to the connection URL but it didn't work. I added ?ssl=true instead and now it works perfectly.
Here's an example Heroku PostgreSQL connection URL that works:
postgres://user:password#ec2-12-34-56-78.compute-99.amazonaws.com:port/databasename?ssl=true
Add or edit the following line in your postgresql.conf file :
listen_addresses = '*'
Add the following line as the first line of pg_hba.conf. It allows access to all databases for all users with an encrypted password:
TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 0.0.0.0/0 md5
Restart postgresql service:
net stop postgresql-9.0 & net start postgresql-9.0
(version should be based on your installation) -- On windows (run cmd as an administrator).
sudo service start postgresql -- On linux (or according to your linux distribution.)
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});

Resources