pyodbc cannot connect to on-premises MSSQL server - python-3.x

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)

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 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.

Why can't I connect to Oracle DB with SQLAlchemy?

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.

Getting current userid and password in windows

I am logged into windows 10 where I am trying to run a connection to oracle db as:
import cx_Oracle
import pandas as pd
qry = 'select * from Table11 where rownum < 20'
conn = cx_Oracle.connect('uid', 'pwd', 'myDBOracle')
testData = pd.read_sql(qry, conn)
The username and password for the database myDBOracle is same as my windows authentication. However, windows authentication changes every month. So, I was wondering whether there was a way to recover the current authentication password for windows so that I don't have to worry about my query breaking every month after password change; since the windows authentication change flows to database also.
I am currently doing:
import os
conn = cx_Oracle.connect(os.getlogin(), 'pwd', 'myDBOracle')
If your DB user account was created with IDENTIFIED EXTERNALLY (see external authentication), then connect with:
import cx_Oracle
conn = cx_Oracle.connect(dsn = "the_tns_entry_name")
See cx_Oracle with Windows authentication

Pull Syslog from the server using Groovy in Jira

I am new to Groovy in Jira, and I am trying to pull syslogs off a certain database. I am wondering if anyone can put me to the right direction. I am using the script console to implement this.
I am guessing it will be on the local host. I am given these to access the database server :
-Database server with Port Number
-Database name
-Password
-Application Database User
-Syslog Servers
Are there any tutorials I can use to be able to connect to the database server
Thank you very much,
Groovy provides the Sql class to simplify connecting to JDBC data sources. Here's an example.
import groovy.sql.Sql
def jdbc = 'jdbc:h2:mem:'
def db = Sql.newInstance(jdbc, 'org.h2.Driver')
def foos = db.rows('select foo from bar')
...
db.close() // Done with connection
The driver and JDBC connection string depends on the DBMS you're connecting to (MySQL, PostgreSQL, etc).
PostgreSQL example
Here's how to connect to PostgreSQL. The code below uses Groovy's Grape to download dependencies.
Note: #GrabConfig is required to load the org.postgresql.Driver class in a way that allows jdbc to find it.
#Grab('org.postgresql:postgresql:9.3-1101-jdbc41')
#GrabConfig(systemClassLoader=true)
import groovy.sql.Sql
def host = '192.168.1.1'
def port = 5432
def dbname = 'foo'
def user = 'xxx'
def password = 'yyy'
def jdbc = "jdbc:postgresql://${host}:${port}/${dbname}"
def db = Sql.newInstance(jdbc, user, password, 'org.postgresql.Driver')

Resources