Why can't I connect to Oracle DB with SQLAlchemy? - python-3.x

I'm trying to connect to a oracle DB with SQLAlchemy however I get the following error:
ORA-12545: Connect failed because target host or object does not exist
Note that the code running this is on a docker container that is located on a vm in GCP.
I tried using tools like telnet, curl, nmap, etc and they all are able to connect/say open. So I don't see why connecting through python would all of a sudden make it not visible.
Here is the code that is used to try to connect.
from sqlalchemy.orm.session import sessionmaker
from framework.db import BuildOracleConnection
Creds_Oracle = {
'userName': 'urname',
'password': 'pass',
'host': '10.10.10.10',
'port': '1521',
'serviceName': 'svcName'
}
Conn_Oracle = BuildOracleConnection(Creds_Oracle)
metaConn = sessionmaker(bind=Conn_Oracle)
metaSession = metaConn()
sql = 'select * from table'
sql = sql.replace('\n', ' ')
sourceExtract = metaSession.execute(sql)
The part that throws the error is the last line.
I expect to be able to connect but instead I get the following error:
ORA-12545: Connect failed because target host or object does not exist.

For some reason I wasn't able to connect directly to the loadbalancer, instead I had to connect to the nodes themselves.

Related

load pandas dataframe into Redshift

I am trying to load the pandas dataframe into Redshift, but it keeps giving me an error. Please guide me on the same. Need help on correcting the cluster configuration to make it work successfully.
Below is my code and error traceback:
from sqlalchemy import create_engine
import pyodbc
import psycopg2
username = "#####"
host = "redshift-cluster-****.*****.ap-south-1.redshift.amazonaws.com"
driver = "Amazon Redshift (x64)"
port = 5439
pwd = "******"
db = "dev"
table = "tablename"
rs_engine = create_engine(f"postgresql://{username}:{pwd}#{host}:{port}/{db}")
df.to_sql(table, con=rs_engine, if_exists='replace',index=False)
Traceback:
OperationalError: (psycopg2.OperationalError) connection to server at "redshift-cluster-****.****.ap-south-1.redshift.amazonaws.com" (3.109.77.136), port 5439 failed: Connection timed out (0x0000274C/10060)
Is the server running on that host and accepting TCP/IP connections?
Even tried the below options, but getting the same error,
rs_engine = create_engine(f"redshift+psycopg2://{username}#{host}:{port}/{db}")
rs_engine = create_engine(f"postgresql+psycopg2://{username}:{pwd}#{host}:{port}/{db}")
rs_engine = redshift_connector.connect(
host='redshift-cluster-####.****.ap-south-1.redshift.amazonaws.com',
database='dev',
user='****',
password='#####'
)
Also, have the Public accessible setting Enabled in Redshift cluster. Still unable to connect and load the data.
UPDATE:
Also tried using ODBC Driver, but getting the same error,
import pyodbc
cnxn = pyodbc.connect(Driver=driver,
Server=host,
Database=db,
UID=username,PWD=pwd,Port=port)
When tried to setup using ODBC Datasources app, getting the same error,

Connecting to Aurora Postgres (Babelfish, 1433)

I'm attempting to connect to a new Aurora PostgreSQL instance with Babelfish enabled.
NOTE: I am able to connect to the instance using the pg library through the normal port 5432 (the Postgres TDAS endpoint).
However, for this test, I am attempting to connect through the Babelfish TDS endpoint (1433) using the standard mssql package.
If I specify a database name (it is correct), I receive the error 'database "postgres" does not exist':
var config = {
server: 'xxx.us-east-1.rds.amazonaws.com',
database: 'postgres',
user: 'xxx',
password: 'xxx'
};
and the connection closes since the connection fails.
if I omit the database property in the config, like:
var config = {
server: 'xxx.us-east-1.rds.amazonaws.com',
user: 'xxx',
password: 'xxx'
};
It will connect. Also, I can use that connection to query basic things like SELECT CURRENT_TIMESTAMP and it works!
However, I can't access any tables.
If I run:
SELECT COUNT(1) FROM PERSON
I receive an error 'relation "person" does not exist'.
If I dot-notate it:
SELECT COUNT(1) FROM postgres.dbo."PERSON"
I receive an error "Cross DB query is not supported".
So, I can't connect to the specific database directly and if I connect without specifying a database, I can't cross-query to the table.
Any one done this yet?
Or, if not, any ideas on helping me figure out what to try next? I'm out of ideas.
Babelfish databases (that you connect to on port 1433) have nothing to do with PostgreSQL databases (port 5432). Essentially, all of Babelfish lives within a single PostgreSQL database (parameter babelfishpg_tsql.database_name).
You seem to have a single-db setup, because Cross DB query is not supported. With such a setup, you can only have a single database via port 1433 (apart from master and tempdb). You have to use CREATE DATABASE to create that single database (if it isn't already created; ask sys.databases).
I can't tell if it is supported to create a table in PostgreSQL (port 5432) and use it on port 1433 (the other way around is fine), but if so, you have to create it in a schema that you created with CREATE SCHEMA while connected on port 1433.
The answer was that I should be connecting to database "master".
Even though there is no database titled master in the instance, you still do connect to it.
Once connected, running the following:
select current_database();
This will indicate you are connected to database "babelfish_db".
I don't know how that works or why a database would have an undocumented alias.
The bigger answer here is that cross-DB object references are not currently supported in Babelfish, outside your current SQL Server database.
This is currently being worked on. Stay tuned.

How to specify tls version 'TLSv1.2' when connecting to MySQL using Python peewee

How do I connect to a MySQL database using Python's peewee library while specifying the tls-versions ['TLSv1.1', 'TLSv1.2'] as shown here and here in the MySQL documentation?
I can successfully connect to the MySQL database using mysql.connector as shown here:
import mysql.connector
config = {'database': 'dbname',
'user': 'username_so',
'password': 'psswrd',
'host': 'maria####-##-###-##.###.####.com',
'port': 3306,
'tls_versions': ['TLSv1.1', 'TLSv1.2']}
cnx = mysql.connector.connect(**config)
cnx.close()
However, I am unable to pass the 'tls_versions' parameter to peewee when establishing a connection. As a result, I get an error message:
ImproperlyConfigured: MySQL driver not installed!
I am pretty sure that the problem is with specifying the tls versions in peewee because I was getting the same error message with mysql.connector before I added in the additional 'tls_versions' parameter.
Here is the code I am using in peewee that is failing:
db = MySQLDatabase(**config)
db.get_tables() # This function and any other that connects to the db gets the same error message specified above
My Setup:
Linux
Python 3.7
peewee==3.13.3
mysql-connector-python==8.0.21
As I responded to your github issue:
You need to use playhouse.mysql_ext.MySQLConnectorDatabase to connect using the mysql-connector driver. That will resolve your issue.

Connecting to Azure PostgreSQL server from python psycopg2 client

I have trouble connecting to the Azure postgres database from python. I am following the guide here - https://learn.microsoft.com/cs-cz/azure/postgresql/connect-python
I have basically the same code for setting up the connection.
But the psycopg2 and SQLalchemy throw me the same error:
OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
I am able to connect to the instance by other client tools like dbeaver but from python it does not work.
When I investigate in Postgres logs I can see that the server actually authorized the connection but the next line says
could not receive data from client: An existing connection was forcibly closed by the remote host.
Python is 3.7
psycopg's version is 2.8.5
Azure Postgres region is in West Europe
Does someone has any suggestion on what should I try to make it work?
Thank you!
EDIT:
The issue resolved itself. I tried the same setup a few days later and it started working. Might have been something wrong with the Azure West Europe.
I had this issue too. I think I read somewhere (I forget where) that Azure has an issue with the # you have to for the username (user#serverName).
I created variables and an f-string and then it worked OK.
import sqlalchemy
username = 'user#server_name'
password = 'PassWord!'
host = 'server_name.postgres.database.azure.com'
database = 'your_database'
conn_str = f'postgresql+psycopg2://{username}:{password}#{host}/{database}'
After that:
engine = sqlalchemy.create_engine(conn_str, pool_pre_ping=True)
conn = engine.connect()
Test it with a simple SQL statement.
sql = 'SELECT * FROM public.some_table;'
results = conn.engine.execute(sql)
This was a connection in UK South. Before that it did complain about the format of the username having to use #, although the username was correct, as tested from the command line with PSQL and another SQL client.

pyodbc cannot connect to on-premises MSSQL server

I want to connect to MSSQL server2012 from Python using pyodbc on a Debian8 host. The current code already works for Azure SQL server but not for local SQL server.
The connection string looks like this:
import pyodbc
server = 'IP'
database = 'DB_Name'
username = 'user'
password = 'pass'
driver = '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
I had to use IP address because I could not reach SQL server via name. I can connect to the server with SSMS on a windows machine. I have tried the following combinations as well:
server='sqlservername\instancename'
server='IP, port'
server='IP\instancename'
driver='SQL Server'
Integrated_Security=false;
Trusted_Connection=no;
but none of them worked. I got either Login timeout or error in registering user. I run the same python code on windows machine without any problem.
Finally i found the correct combination. Besides thanks to #GordThompson the TCP/IP was enabled but on another port.
import pyodbc
server = 'IP\InstanceName, port'
database = 'DB_Name'
username = 'user'
password = 'pass'
driver = '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)

Resources