Python - Postgres - psycopg2 - ThreadedConnectionPool - Connect to primary in HA cluster - python

Does the Python/psycopg2/ThreadedConnectionPool accept a parameter that tells it to only connect to the primary server in an Postgres HA cluster? The equivalent jdbc connection string would look something like, note the targetServerType=primary:
jdbc:postgresql://serverName1:5432,serverName2:5432,serverName3:5432/dataBaseName?loadBalanceHosts=false&targetServerType=primary
How can I pass the targetServerType to the ThreadedConnectionPool in the connection setup below:
self._pg_pool = psycopg2.pool.ThreadedConnectionPool(
minconn = 1,
maxconn = 2,
host = 'serverName1, serverName2, serverName3',
database = 'dataBaseName1',
user = 'userName',
password = 'passWord',
port = "5432",
application_name = 'MyTestApp'
)

Related

How connect to database and get data via python like in R

I new in python. I need connect to mysql database and get data. Before that, I easily took data via R using rodbslike this.
library(DBI)
library(RMySQL)
db_user = 'k'
db_password = 'F6'
db_name = 'meg'
db_table = 'dat'
db_host = 'my.g2s' # for local access
db_port = 3306
# 3. Read data from db
mydbV7 = dbConnect(MySQL(), user = db_user, password = db_password,
dbname = db_name, host = db_host, port = db_port)
sV7 = paste0("select * from ", db_table)
rsV7 = dbSendQuery(mydbV7, sV7)
df = fetch(rsV7, n = -1)
but when i tried to implement the same principle in python i get errors
import pyodbc
>>> db_user = 'k'
>>> db_password = 'F6'
>>> db_name = 'meg'
>>> db_table = 'dat'
>>> db_host = 'my.g2s' # for local access
>>> db_port = 3306
>>> mydbV7 = dbConnect(MySQL(), user = db_user, password = db_password,
... dbname = db_name, host = db_host, port = db_port)
File "<stdin>", line 2
... dbname = db_name, host = db_host, port = db_port)
^
SyntaxError: positional argument follows keyword argument
How can i correct get data via python 3.9?
As always, I appreciate any of your help.
As describe in the MySQL documentation, you can import mysql.connector and then use :
cnx = mysql.connector.connect(user = 'scott',
password = 'password',
host = '127.0.0.1',
database = 'employees')
Of course, you have to change the values with yours.
cnx will then represent the connection with your database.

How to parse variable values in stored proc from dataframe using python

I have written a code to pick specific values from email body and store it in dataframe now the next step is to store those values in oracle database for that i am using sqlalchemy but i am not sure how can i pass those values to store proc like below
call CORE_VALUATIONS.VALUATIONS.INSERTEQCLOSINGPRICE("SGEPSBSH",to_date('"&TEXT(2022-06-01,"DDMMMYYYY")&"','ddmonyyyy'),"111.9852",NULL,NULL);
from sqlalchemy.engine import create_engine
import datetime
today = datetime.date.today()
DIALECT = 'oracle'
SQL_DRIVER = 'cx_oracle'
USERNAME = 'robinhood' # enter your username
PASSWORD = 'XXXXXX' # enter your password
HOST = 'pv-prod-orc-01.XXXXX.com' # enter the oracle db host url
PORT = 1521 # enter the oracle port number
SERVICE = 'XXXX_APP.ec2.internal' # enter the oracle db service name
ENGINE_PATH_WIN_AUTH = DIALECT + '+' + SQL_DRIVER + '://' + USERNAME + ':' + PASSWORD + '#' + HOST + ':' + str(
PORT) + '/?service_name=' + SERVICE
engine = create_engine(ENGINE_PATH_WIN_AUTH)
# test query
query = """
call CORE_VALUATIONS.VALUATIONS.INSERTEQCLOSINGPRICE("**SGEPSBSH**",to_date('"&TEXT(**2022-06-01**,"DDMMMYYYY")&"','ddmonyyyy'),"**111.9852**",NULL,NULL);
"""
con = engine.connect()
outpt = con.execute(query)
con.close()
how can i pass those values to store proc
Call .execute(sql_text, dict_of_param_values), e.g., something like
import sqlalchemy as sa
# …
sql_text = sa.text(
"call CORE_VALUATIONS.VALUATIONS.INSERTEQCLOSINGPRICE(:param1, :param2, :param3, NULL, NULL);"
)
dict_of_param_values = dict(param1="SGEPSBSH", param2="2022-06-01", param3=111.9852)
with engine.begin() as conn:
conn.execute(sql_text, dict_of_param_values)
# (transaction will automatically commit when `with` block exits)

Unable to fetch data from PostgreSQL table connected using Python

I can fetch the data using this command:
connection = psg.connect( database = "Test" , host="localhost", user="postgres", password="password")
data_1 = psql.read_sql('SELECT * FROM table_1 WHERE id IN (101 , 102)', connection)
But when I run the command below, it gives me an error. A user will put the dynamic ID values and it'll show the data corresponding to the respected IDs. That's why a variable is created which will be on user interface.
connection = psg.connect( database = "Test" , host="localhost", user="postgres", password="password")
variable_p = (108 ) # 108 is the id column value.
data_1 = psql.read_sql('SELECT * FROM table_1 WHERE id IN (variable_p[0])', connection)
Error - Column variable_p does not exist.
you are not using your variable variable_p in your read sql string. You can use f-strings here:
connection = psg.connect( database = "Test" , host="localhost", user="postgres", password="password")
variable_p = 108 # don't need parentheses for single value
data_1 = psql.read_sql(f'SELECT * FROM table_1 WHERE id = {variable_p}', connection)
But here you are vulnerable to SQL injection. So try parametrising your query to make it safe.
Parametrising and using WHERE IN this might be something like (untested):
variable_p = (1, 2, 3)
data_1 = psql.read_sql('SELECT * FROM table_1 WHERE id IN %s', connection, params=variable_p)

Create random users on SQL Server 2019 with Python

I would like to know if there's a way to create an user with python on a SQL Server 2019? Because I succeed to connect and create a database on the server but I can't connected to the database then...
Here's a bit of my code.
def create_user(self):
global date
utilisateur = self.nom_utilisateur.get()
password = self.password.get()
dbname = utilisateur + str(date.year)
mydb = pyodbc.connect(driver='{SQL Server}', host='DELL-G3\SQLEXPRESS', iud=utilisateur, psw=password)
mydb.autocommit = True
cursor = mydb.cursor()
cursor.execute("CREATE DATABASE %s" % dbname)
try:
mkuser = utilisateur
mkpass = password
creation = "CREATE USER '%s'#'DELL-G3\SQLEXPRESS' IDENTIFIED BY '%s' FROM EXTERNAL PROVIDER" % (mkuser, mkpass)
cursor.execute(creation)
grant = "GRANT SELECT, INSERT ON *.* TO '%s'#'DELL-G3\SQLEXPRESS'" % (mkuser)
cursor.execute(grant)
except sqlite3.OperationalError:
print('error')
mydb.close()

Not able to iterate mongo object in python

I am trying to get the records (books), which are present the in Mongo DB collection "books". I am using pymongo and flask.
Below is my code. In the code, if I remove the query update line (query.update({'author': 'sachin'})), it works fine.
What is the problem while updating the query dict?
from pymongo import connection as pymongo_client
import urllib
def get_books(query, skip_val=0, limit=None):
conn, client = _connect()
if limit:
result = client.books.find(query).skip(skip_val).limit(limit)
else:
result = client.books.find(query)
conn.close()
return result
def _connect():
user = "root"
password = "password"
host = "172.17.1.14"
port = "27017"
db_name = "books"
auth_database = "admin"
if user and password and auth_database:
uri = "mongodb://%s:%s#%s:%s/%s" % (
user, urllib.quote_plus(password), host, port, auth_database)
else:
uri = "mongodb://%s:%s" % (host, port)
conn = pymongo_client.Connection(uri, j=True)
db = conn[db_name]
return conn, db
query = {'project_name': 'cricket'}
books = get_books(query)
query.update({'author': 'sachin'})
for book in books:
print book

Categories