How to connect to PostgreSQL using SQLAlchemy? - python

I am trying to connect to my PostgreSQL database using SLQAlchemy. I have installed psycopg2 and tried to connect to database with a raw python code and it worked but this time I am trying using SQLAlchemy. I read the documentation from SQLAlchemy docs. This might look very simple and I could not find a solution in the internet either for this specific one.
This is my code, so simple:
import psycopg2
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine('postgresql+psycopg2://nysa:mypassword#127.0.0.1:46015/nysa')
db = scoped_session(sessionmaker(bind=engine))
connection = engine.connect()
result = connection.execute('select*from table')
print(result)
When running the *.py file nothing happens and there are no errors and I tried to find the issue with pycharm's debugger and I noticed that in the line connection = engine.connect() gets stuck like in a infinite loop. I am pretty sure that my database credentials are correct. How do I connect to database?

Got it. When I opening pgAdmin it shows a different port and I thought that I had to put that port in the code, that is why It did not work. Worked with 127.0.0.1:5432.

Related

Flask SQLAlchemy initial database connection extremely slow

When I make an initial database connection with FlaskSQLAlchemy it is extremely slow, sometimes taking a minute or more to successfully connect and execute a query. Any subsequent database calls after the initial connection are fast. From my understanding this has something to do with "lazy loading". How can I "force" this initial connection to occur earlier, or speed up the connection altogether?
I am using MS SQL Server for my database, and pyodbc for my DB API. The application is deployed on Windows IIS server, although this problem occurs even while connecting locally - it is exacerbated while deployed on IIS.
Use latest version of PYODBC driver. First install python package.
pip install pyodbc
If you use following code, then you will get fast Response from MSSQL database for sure.
import urllib
params = urllib.parse.quote_plus(
'DRIVER={ODBC Driver 17 for SQL Server};SERVER=server-name;DATABASE=database-name;UID=sa;PWD=****;Trusted_Connection=yes;'
)
class Config:
DEBUG = True
SECRET_KEY = "E9738F40-4210ACAD94B"
SQLALCHEMY_DATABASE_URI = "mssql+pyodbc:///?odbc_connect=%s" % params
Make sure your ODBC driver is accurate in your system. If you want to get knowledge about ODBC driver, then run following query in cmd command.
import pyodbc
pyodbc.drivers()
Then put following code in your init.py file
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(Config)
Above things will be giving your ORM (Object Relation Mapper) which will gives you a technique of mapping object parameters to the underlying RDBMS table structure. An ORM API provides methods to perform CRUD operations without having to write raw SQL statements.

Connecting Heroku Database to SQLAlchemy

so as part of a Web Programming MOOC, I have to connect to a PostgreSQL Database hosted on Heroku using Python and SQLalchemy. I have spent a lot of hours trying to do this to no avail. My main problem is that I'm not being able to connect to the Database, because every time a try to run a script to update or just check the Database I get "Is the server running on host "ec2-174-129-35-61.compute-1.amazonaws.com" (174.129.35.61) and accepting
TCP/IP connections on port 5432?" in the command prompt.
I've done a lot of research trying to figure it out and it seems that maybe the URI of my Database is not set to accept public connections. But I tried setting ssl = require and still haven't been able to solve the problem. Maybe I didn't do it right?
I have come to think that maybe I am doing something wrong with the code, or that I should try to connect through Heroku's CLI and not the command prompt but I am not sure. This is my simple test code:
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
if not os.getenv("DATABASE_URL"):
raise RuntimeError("DATABASE_URL is not set")
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
db.execute("INSERT INTO books VALUES (1, 1, 1, 1)")
db.commit()
I set the DATABASE_URL environment variable in the command prompt to the Database's URI but maybe I did it wrong? I also tried just using the URI in the code itself but still could not make it work.
This is my first time using Databases with Heroku so I am complete neewbie, maybe I am doing something else wrong?. I appreciate all the help that you guys can give. Thanks in advance
1) Check, did you copied all link, including postgres://
2) Maybe you had made a mistake during setting DATABASE_URL. So, try to add your URL directly to the script:
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = "postgres://ennjrjkrfsb____all_code_of_key_.....___compute.amazonaws.com:5432/ddo7541ka3hio7"
db = scoped_session(sessionmaker(bind=engine))
db.execute("INSERT INTO books VALUES (1, 1, 1, 1)")
db.commit()
3) if you are using linux, try to open your database, using command psql postgres://ennjrjkrfsb____all_code_of_key_.....___compute.amazonaws.com:5432/ddo7541ka3hio7
4) Try to access to your database in any way via other internet provider (may be your provider or sys admin in your office restricted access to Heroku)
Update: Try to use free online IDE with interpreter (based on Linux), for example, https://cs50.io - from this site you can access to Heroku postgres
postgres dialect does not support any more in SQLAlchemy v1.4.x above, and it turns into postgresql, so the final connection string must be postgresql://username:password#host:port/database
Here is my working connection from SQLAlchemy to Heroku Postgresql
database_url = os.environ('DATABASE_URL')
if database_url.startswith('postgres://'):
database_url.replace('postgres://', 'postgresql://')
engine = create_engine(database_url)
# rest of your codes...
here is my reference Heroku Issue

sqlacodegen doesn't make any Tables

I'm running a PostgreSQL database on a server and I'm trying to connect to it using SQLAlchemy. I found that sqlacodegen was a good tool to generate the MetaData object automatically along with its Tables. But when I try and run sqlacodgen postgresql+psycopg2://username:password#host:5432/dbname, I only get this:
# coding: utf-8
from sqlalchemy import MetaData
metadata = MetaData()
The connection string is definitely correct, and the database is up - I ran a small Python script using that connection string to connect to the database and used execute to run a query on it, and it returns exactly what I expected.
I'm not sure where even to start with debugging this. What could I do to see what's wrong? Is there some sort of requirement that sqlacodegen has that I'm missing?
As it turns out this problem was unrelated to sqlacodegen. All the tables in my database are prefixed with dbname. Passing in --schema dbname works.

Unknown database error after creating it (sqlalchemy)

I'm getting the next error while trying to run my Flask app:
sqlalchemy.exc.OperationalError
OperationalError: (_mysql_exceptions.OperationalError) (1049, "Unknown database '/home/gerardo/Documentos/python_web_dev/flask-intro/app2.db'")
so it seems like there is no database.. but I ran the next scripts using sqlalchemy_utils and everything was ok:
engine = create_engine("mysql://root:#localhost/home/gerardo/Documentos/python_web_dev/flask-intro/app2.db")
create_database(engine.url)
but still I get the error..
You have confused MySQL, a database server, with SQLite, a database in a file. You created a SQLite file, but are trying to tell MySQL to connect to it, which makes no sense.
Use the sqlite dialect in the connection string.
You can avoid typing the whole path (and tying the app to that path) by pointing to the file relative to the app's location.
import os
db_path = os.path.realpath(os.path.join(os.path.dirname(__file__), 'app2.db'))
engine = create_engine('sqlite://{}'.format(db_path))
Consider using Flask-SQLAlchemy rather than trying to manage the database yourself.

Database testing in python, postgresql

How do you unit test your python DAL that is using postgresql.
In sqlite you could create in-memory database for every test but this cannot be done for postgresql.
I want a library that could be used to setup a database and clean it once the test is done.
I am using Sqlalchemy as my ORM.
pg_tmp(1) is a utility intended to make this task easy. Here is how you might start up a new connection with SQLAlchemy:
from subprocess import check_output
from sqlalchemy import create_engine
url = check_output(['pg_tmp', '-t'])
engine = create_engine(url)
This will spin up a new database that is automatically destroyed in 60 seconds. If a connection is open pg_tmp will wait until all active connections are closed.
Have you tried testing.postgresql?
You can use nose to write your tests, then just use SQLAlchemy to create and clean the test database in your setup/teardown methods.
There's QuickPiggy too, which is capable of cleaning up after itself.
From the docs:
A makeshift PostgresSQL instance can be obtained quite easily:
pig = quickpiggy.Piggy(volatile=True, create_db='somedb')
conn = psycopg2.connect(pig.dsnstring())

Categories

Resources