Connect to informix with python's ibm_db - python

I'm pretty new to python. I'm trying to connect to an informix server using python and ibm_db.connect(). However I can't seem to succeed and the error messages don't help.
Using java and jdbc I can connect successfully with the following connection url:
jdbc:informix-sqli://10.20.30.40:1234/mydb:INFORMIXSERVER=foo_bar;USER=user;PASSWORD=pass;
My attempt at using ibm_db is:
ibm_db.connect('HOSTNAME=10.20.30.40;PORT=1234;DATABASE=mydb;PROTOCOL=ONSOCTCP;UID=user;PASSWORD=pass;', '', '')
But it gives an error (Exception: [IBM][CLI Driver] SQL0902C A system error occurred. Subsequent SQL statements cannot be processed. IBM software support reason code: "". SQLSTATE=58005)
I would like if possible an equivalent string I can put in as the first argument to
ibm_db.connect('', '', '')
So I can connect with python.

Please check https://code.google.com/p/ibm-db/issues/detail?id=116&can=1&q=ONSOCTCP, This might be helpful for you. If you still facing issue then you can post your query to https://groups.google.com/forum/#!forum/ibm_db for quick response.

Protocol onsoctcp is not supported by ibm_db.
Please check https://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.admin.doc/ids_admin_0207.htm, This can help you to configure a DDRA (tcpip) access to your database.

I have faced the same issue with ibm_db and now I use jayDeBeApi to connect to Informix through python. it requires java JDBC driver and application is work like a charm.
https://pypi.org/project/JayDeBeApi/#:~:text=The%20JayDeBeApi%20module%20allows%20you,of%20the%20Java%20JDBC%20driver.

Your JDBC connection string points to a SQLI Informix listener, but the 'ibm_db' python module uses DRDA (IBM Data Server Driver) to connect to the Informix engine.
Informix allows both SQLI and DRDA clients (and others like MongoDB). SQLI is the 'native' Informix protocol and supports all the Informix server features and data types. DRDA is what other IBM databases use (like DB2). It does have some limitations in terms of what types you can use.
You have two options:
Configure the Informix server to listen for DRDA connections in a another port (basically, create an DBALIASES using 'drsoctcp') as described here:
https://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.admin.doc/ids_admin_0207.htm
Or leave the server as it is, and use a different Python module, one that uses SQLI like 'IfxPy'
https://github.com/OpenInformix/IfxPy

Related

Exasol_Error: I keep getting Exasol connection error timed out

I am trying to connect to my Exasol SaaS database, I tried via these tools(TALEND, DBVISUALIZER, POWERBI) and via python but I cannot connect and I keep getting the same error.
I saw another post on Exasol community https://community.exasol.com/t5/discussion-forum/exaconnectionfailederror/m-p/8049#M1855 of this type of error but it doesn't explain exactly what was done to fix the error. I tried via the ODBC Data Source administrator(64-bit) too but still the same error. Maybe its an connection issue with my pc self but I'm not sure or maybe I am just inserting wrong values I don't know.
Oh the values I inserted are the recommended ones from what Exasol docs states and I have removed anything about proxy or vpn.
I put my errors under. I tried via different devices and I get the same error I really don't know what I can do any more, so any help will be greatly appreciated.
Note: I am using the Exasol SaaS database and I am currently on the trial mode so I am not sure if this is limiting me.
**Errors: **
Error message odbc exasol: [EXASOL][EXASolution driver]connection attempt timed out.
Error message Talend : Connection failure. You must change the Database Settings.
java.lang.RuntimeException: com.exasol.jdbc.ConnectFailed: connect timed out ->
Caused by: com.exasol.jdbc.ConnectFailed: connect timed out
Error message pyexasol : socket.timeout: timed out
Error message dbvisualizer : java.net.SocketTimeoutException: Connect timed out com.exasol.jdbc.ConnectFailed: java.net.SocketTimeoutException: Connect timed out
Error message Power BI desktop : Details: "ODBC: ERROR [HYT00][EXASOL][EXASolution driver]Connection attempt timed out."
My applications versions:
DbVisualizer Free 14.0.1 (build: 3540)
Talend Open Studio Data integration(8.0.1.2021119_1610)
java version -> jdk-16.0.02
Power BI -> Version: 2.110.1085.0 64-bit (October 2022)
ODBC : exasolodbc x64 7.1.14
JDBC : exasoljdbc 7.1.14
Python: python 3.8.10 -> pyexasol : 0.25.1
The error means that the client is not able to reach the host for some reason. Try the following:
Make sure the database is still online (they auto-shutdown after 2 hours if there is no activity by default)
Check that the IP Address of the host you are connecting with is added to the allow list in the SaaS UI. (see the docs)
Check if your host is able to reach the host and port specified in the SaaS UI (for example using telnet on port 8563). Maybe some firewall is preventing access to the database?
So I did more digging. actually I have no idea what the issue was.
Talend:
I made a connection via JDBC in Talend with the help of exasol-support. The DBType Exasol in talend doesn't work for some reason, its not known if it's talend side or Exasol side, maybe this will be updated in the future. Just remember in the jdbc url type this: "jdbc:exa:yourconnectionstring", don't forget the "exa".
PowerBI:
I tried the connection string with fingerprint method that worked for me. Just put the fingerprint with the connection string and it should connect.
https://exasol.my.site.com/s/article/PowerBI-Encryption-Fingerprint-Issue-in-Exasol-7-1?language=en_US
DBvisualizer:
I had a wrong in connection string.
Python:
I had a wrong in connection string.
Hopefully this helps someone.

connect to the database via Python

I am trying to connect to the database via Python ?
I use oracle with the sqldevolper and have no problems there, but now when connecting between oracle and python the problem appears.
When I try to connect, I get the following error :
cx_Oracle.DatabaseError: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
I tried the script with spyder:
import cx_Oracle
con = cx_Oracle.connect(
user="abc",
password ="pass",
dsn="....")
ORA-12514 means you are connecting to a listener, but the service name you specified in your connection string, isn't known by the listener. As you omitted the definition of the connect string (from SQL Developer and Python), hard to say if there is a typo or what caused it...
On the server you can check with "lsnrctl status" on the known services... maybe that gives you a hint, what might be wrong.

Can someone else connect to my SQL Server using Windows Authentication or any other method, or am I the only one?

Can someone else connect to my SQL Server using my Windows Authentication or am I the only one? I saw that there is an option for "allow remote connections" in SSMS, so I'm wondering if someone has my connection credentials, such as server name and database, can they connect to it?
Is the server name "sensitive information" or does it not matter? I'm wondering because I always get hesitant typing out my server name which is DESKTOP-xxxxx (x in place of the actual numbers, which is the thing I'm not sure is sensitive or not)
example:
conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
'Server=DESKTOP-xxxxxx;' **<--------is this sensitive info?**
'Database=Test_Database;' **<--------is this sensitive info?**
'Trusted_Connection=yes;')
Ignore the SSMS allow remote connection option. Per the documentation:
This configuration option is an obscure SQL Server to SQL Server
communication feature that is deprecated
Use SQL Server Configuration Manager to view, enable, or disable protocols as desired. Remote TCP/IP and Named pipes are disabled by default.
If someone besides you knows your Windows credentials, you have bigger issues. Although they will not be able to connect to SQL Server remotely when the protocols are disabled, they could still get to your database via other means (e.g. RDP into your machine and access SQL Server locally).
The name of your machine could be considered sensitive but it's easily discoverable (e.g. DNS). You generally want multiple layers, which include firewalls and surface area reduction (e.g. disabled RDP), and perhaps obfuscation (non-standard SQL ports) as well for protecting particularly sensitive data.

connect to Wonderware Historian OLE DB using python 3 and OSX or Linux

Could anyone share a functional connection method to Wonderware's Historian using python3 on OSX (or linux)?
Historian is apparently a Microsoft SQL Server OLE DB (see pg102 of http://www.logic-control.com/datasheets/1/Historian/HistorianConcepts.pdf).
Another SO post suggests that the only python library available capable of connecting to an OLE DB is 'adodbapi' (Connecting to Microsoft SQL server using Python)
an attempt in code (using default RO credentials):
import adodbapi
ServerName = "ServerName"
MSQLDatabase = "Runtime"
username = "aaUser"
password = "pwUser"
conn = adodbapi.connect("PROVIDER=INSQL;Data Source={0};Database={1};trusted_connection=yes;UID={2};PWD{3};".format(ServerName,MSQLDatabase,username,password))
That gives an error:
adodbapi.apibase.OperationalError: (InterfaceError("Windows COM Error: Dispatch('ADODB.Connection') failed.",)...
The error is probably due to the absence and unavailability of the pywin32 package, which is apparently Windows only (Pywin32 (com objects) on Mac)
Tips appreciated. I highly suspect that the Microsoft vs Mac/Linux worlds just can't be bridged in this situation.
You should be able to access an MS SQL Server database (e.g. Wonderware Historian database) with Python.
Things to check:
For ODBC, "Trusted Connection" setting should be "No". Trusted connection means that it tries to use Windows Authentication to log in. You want to use a username and password instead. I think for OLE DB you set "Integrated Security = SSPI" instead.
Connection String (Username, Server Hostname/IP, Database name, Correct Port, Syntax)
Port (make sure you use the correct port - may be a non-standard port)
Firewalls - make sure that any firewalls are set up to allow access
If you're using the hostname, make sure DNS is working (e.g. can you ping the server?)
You may need to install an ODBC driver for Linux and pyodbc. ODBC is a more open standard. As you've pointed out, OLEDB is COM-based (e.g. Windows-based) so I'm not sure if it would be compatible.
I am able to access a Wonderware server using Python3 both through sqlalchemy and pyodbc on Linux and Windows - I don't have a Mac so you're on your own there. I've read that there are other drivers available, but I don't have any experience with them. If you have any suggestions here I'd be glad to hear them.
This is how I modularize the functionality of pyodbc. Essentially, I've defined a function in our code that sets up the sql engine conncetion:
def get_conn():
conn_pyodbc = pyodbc.connect(DSN=<myDSN>, UID=<user>, PWD=<pass>)
return conn_pyodbc
And I use the connection as such:
def executeQuery(sql_query):
with get_conn() as conn:
df = pd.read_sql(sql_query, conn)
Using the context manager just seems like an easier way of handling opening and closing database connections.
As far as setting up the DSN, I needed to install the Microsoft ODBC driver, which was easy enough to do by following some links I found online. After a successful installation I edited the /etc/odbc.init and /etc/odbcinst.ini files manually such that they look like this now:
$ cat /etc/odbc.ini
[myDSN]
Driver=ODBC Driver 13 for SQL Server
Description=Awesome server
Trace=No
Server=<serverIP>
and
$ cat odbcinst.ini
[ODBC Driver 13 for SQL Server]
Description=Microsoft ODBC Driver 13 for SQL Server
Driver=/opt/microsoft/msodbcsql/lib64/libmsodbcsql-13.1.so.9.1
UsageCount=1
In my experience, you can use 'Trusted Connection', but that means that your computer will try to connect to the server using it's Windows credentials. These work if you are logged into a Windows machine that has access to the data in question. Running on Linux is a different story, so I keep to the user/password combo.
Anymore questions feel free to ask.

How to secure pandas methods when communicating with a Postgres database?

Python Pandas has both a to_sql and read_sql methods which can be used to communicate with a database.
I am connecting to a Postgres database.
Pandas seems to use SQL Alchemy and psycopg2 to perform the communication.
When using create_engine('mysql+mysqlconnector://user:password#amazon_endpoint:port/database_name',connect_args={'sslmode':'require'}, echo=True) I received an AttributeError: Unsupported argument 'sslmode' error.
I then saw the following posts: 1, 2 and amended the code to be create_engine('mysql+mysqlconnector://user:password#amazon_endpoint:port/database_name?ssl_ca=/home/user/Downloads/rds-combined-ca-bundle.pem'). The connection now works and I am able to connect to the database. Does this means that the connection is now secure? Is there a way I can confirm this?
I don't believe the connection is secure by default, unless your Postgres instance has been configured to require secure connections. First, your instance needs to have been built with SSL support, and then it's a simple matter of telling the client to connect securely. From the documentation:
PostgreSQL has native support for using SSL connections to encrypt client/server communications for increased security. This requires that OpenSSL is installed on both client and server systems and that support in PostgreSQL is enabled at build time (see Chapter 15).
With SSL support compiled in, the PostgreSQL server can be started with SSL enabled by setting the parameter ssl to on in postgresql.conf. The server will listen for both normal and SSL connections on the same TCP port, and will negotiate with any connecting client on whether to use SSL. By default, this is at the client's option; see Section 19.1 about how to set up the server to require use of SSL for some or all connections.
When you use to_sql or read_sql, pass it a SQLAlchemy connection that's been set up with sslmode=require. This question provides the full snippet for setting up your connection:
db = create_engine(
'postgresql+pg8000://user:pass#hostname/dbname',
connect_args={'sslmode':'require'},
echo=True
).connect()

Categories