Postgres sqlalchemy Session creation with schema name - python

I can able to create postgres sqlalchemy connection to 'public' schema.
url = 'postgresql://scott:tiger#localhost:5432/mydatabase'
engine = create_engine(url)
engine.connect()
session = sessionmaker(bind=engine)
Session = session()
Now try to connect non-public schema say 'myschema'.
engine = create_engine(url, connect_args={"schema" : "myschema"})
engine.connect()
session = sessionmaker(bind=engine)
Session = session()
I got error :
TypeError: connect() got an unexpected keyword argument 'schema'
How can i connect postgres by using able statements.
Thank you.

This is valid connection string for specific schema:
engine = create_engine('postgresql://dbuser:dbpass#localhost:5432/dbname', connect_args={'options': '-csearch_path={}'.format('dbschema')})

Related

URI of MySQL for SQLAlchemy for connection without password

Could someone kindly advise how the uri of MySQL for SQLAlchemy for a connection without password should be set?
For the code as below, the pymysql part works, but the SQLAlchemy has the below error. I have tried other uri as well as commented below, all failed.
The database name is "finance_fdata_master"
Thanks a lot
# Using pymysql
import pymysql
dbcon = pymysql.connect(host='localhost', user='root', password='', database='finance_fdata_master')
# Using SQLAlchemy
from os import environ
from sqlalchemy import create_engine
uri = 'mysql+pymysql://root#localhost/finance_fdata_master'
db_uri = environ.get(uri)
engine = create_engine(db_uri, echo=True)
# uri = 'pymysql://root#localhost:3306/finance_fdata_master'
# uri = r'mysql://root#127.0.0.1:3306/finance_fdata_master'
# uri = r'mysql://root:#127.0.0.1:3306/finance_fdata_master'
# uri = r'mysql://root#localhost/finance_fdata_master'
Traceback (most recent call last):
File C:\PythonProjects\TradeAnalysis\Test\TestSQLAlchemy.py:23 in <module>
engine = create_engine(db_uri, echo=True)
File <string>:2 in create_engine
File ~\anaconda3\lib\site-packages\sqlalchemy\util\deprecations.py:309 in warned
return fn(*args, **kwargs)
File ~\anaconda3\lib\site-packages\sqlalchemy\engine\create.py:532 in create_engine
u, plugins, kwargs = u._instantiate_plugins(kwargs)
AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'
Your code defines a connection URL string in the variable uri. Then you look up an environment variable with that name and it doesn't exist, so db_uri is None. Then you pass that (None value) to create_engine() and it fails.
engine = create_engine(uri, echo=True) # not db_uri
will probably work better.

Sql alchemy using connection string with kerberos and impala

Actually I use sql alchemy and impala to connect database with creator like :
def conn():
return connect(host=CONNECTION_PARAMETERS["HOST"],
port=CONNECTION_PARAMETERS["PORT"],
database=CONNECTION_PARAMETERS["DATABASE"],
use_ssl=CONNECTION_PARAMETERS["USE_SSL"],
auth_mechanism=CONNECTION_PARAMETERS["AUTH_MECANISM"])
def get_bde_db():
engine = create_engine('impala://', creator=conn,
pool_size=2,
pool_pre_ping=True,
pool_recycle=3600)
user_session_local = sessionmaker(autocommit=False, autoflush=False, bind=engine)
db = user_session_local()
try:
yield db
finally:
db.close()
But I want to change it, stop use the creator parameter and build a connection string in the create_engine. Is it possible to generate a url, and use url parameter ?
I use impala, kerberos, ssl

How to set `lock_timeout` on a PostgreSQL connection with SQLAlchemy and psycopg2?

With PostgreSQL, you can run this query to set a specific lock_timeout for a session:
SET lock_timeout TO '3s'
I'm wondering if there is a nice way to set this option when setting up a connection with SQLAlchemy. The way I'm instantiating SQLAlchemy sessions is the following:
engine = create_engine('postgresql+psycopg2://{user}:{pswd}#{host}:{port}/{name}')
session = scoped_session(sessionmaker(bind=engine))
I've tried passing it in connect_args but that is not supported:
engine = create_engine(
'postgresql+psycopg2://{user}:{pswd}#{host}:{port}/{name}',
connect_args={'lock_timeout': 3}
)
Is there a way to set this option per-session/connection with SQLAlchemy and psycopg2?
As it turned out, this is the right way to set lock_timeout for a session (note that the value is in milliseconds):
engine = create_engine(
'postgresql+psycopg2://{user}:{pswd}#{host}:{port}/{name}',
connect_args={'options': '-c lock_timeout=3000'}
)

'module' object is not callable with sqlalchemy

I'm totally new using sqlalchemy and postgresql. I read this tutorial to build the following piece of code :
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy import engine
def connect(user, password, db, host='localhost', port=5432):
'''Returns a connection and a metadata object'''
# We connect with the help of the PostgreSQL URL
# postgresql://federer:grandestslam#localhost:5432/tennis
url = 'postgresql://{}:{}#{}:{}/{}'
url = url.format(user, password, host, port, db)
# The return value of create_engine() is our connection object
con = sqlalchemy.create_engine(url, client_encoding='utf8')
# We then bind the connection to MetaData()
meta = sqlalchemy.MetaData(bind=con, reflect=True)
return con, meta
con, meta = connect('federer', 'grandestslam', 'tennis')
con
engine('postgresql://federer:***#localhost:5432/tennis')
meta
MetaData(bind=Engine('postgresql://federer:***#localhost:5432/tennis'))
When running it I have this error :
File "test.py", line 22, in <module>
engine('postgresql://federer:***#localhost:5432/tennis')
TypeError: 'module' object is not callable
what should I do ? thanks !
So, your problem is happening because you've made this call:
from sqlalchemy import engine
And then you've used this later in the file:
engine('postgresql://federer:***#localhost:5432/tennis')
Strangely, in that section, you have some statements that are just con and meta with no assignments or calls or anything. I'm not sure what you're doing there. I would suggest that you check out SQLalchemy's page on engine and connection use to help get you sorted.
It will of course depend on exactly how you've set up your database. I used the declarative_base module in one of my projects, so my process of setting up a session to connect to my DB looks like this:
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# Connect to Database and create database session
engine = create_engine('postgresql://catalog:catalog#localhost/menus')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
And in my database setup file, I've assigned:
Base = declarative_base()
But you'll have to customize it a bit to your particular setup. I hope that helps.
Edit: I see now where those calls to con and meta were coming from, as well as your other confusing lines, it's part of the tutorial you linked to. What he was doing in that tutorial was using the Python interpreter in command line. I'll explain a few of the things he did there in the hope that it helps you some more. Lines beginning with >>> are what he enters in as commands. The other lines are the output he receives back.
>>> con, meta = connect('federer', 'grandestslam', 'tennis') # he creates the connection and meta objects
>>> con # now he calls the connection by itself to have it show that it's connected to his DB
Engine(postgresql://federer:***#localhost:5432/tennis)
>>> meta # here he calls his meta object to show how it, too, is connected
MetaData(bind=Engine(postgresql://federer:***#localhost:5432/tennis))

MySQL Connection not available when use SQLAlchemy(MySQL) and Flask

I'm getting this error sometime (sometime is ok, sometime is wrong):
sqlalchemy.exc.OperationalError: (OperationalError) MySQL Connection not available.
while using session.query
I'm writing a simple server with Flask and SQLAlchemy (MySQL). My app.py like this:
Session = sessionmaker(bind=engine)
session = Session()
#app.route('/foo')
def foo():
try:
session.query(Foo).all()
except Exception:
session.rollback()
Update
I also create new session in another file and call it in app.py
Session = sessionmaker(bind=engine)
session = Session()
def foo_helper(): #call in app.py
session.query(Something).all()
Update 2
My engine:
engine = create_engine('path')
How can I avoid that error?
Thank you!
Make sure the value of ‘pool_recycle option’ is less than your MYSQLs wait_timeout value when using SQLAlchemy ‘create_engine’ function.
engine = create_engine("mysql://username:password#localhost/myDatabase", pool_recycle=3600)
Try to use scoped_session to make your session:
from sqlalchemy.orm import scoped_session, sessionmaker
session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
and close/remove your session after retrieving your data.
session.query(Foo).all()
session.close()

Categories