access postgresql server via python3 fails if (unrequested) db does not exist - python-3.x

psql user was created as part of the following:
postgres=# create database testdb;
postgres=# create user testuser with encrypted password 'testpass';
postgres=# grant all privileges on database testdb to testuser;
postgres=# alter user testuser createdb;
The below python3 script functions if database "testdb" exists.
However the script fails:
(NameError: name 'con' is not defined)
if "testdb" does not exist.
I fail to understand the requirement, as I havent asked to connect to the db yet. any assistance you can offer to help me understand is appreciated in advance. tnx.
# import modules
import psycopg2
# connect to db server
try:
con = psycopg2.connect(
host = "127.0.0.1",
port = "5432",
user = "testuser",
password = "testpass")
except:
print("Unable to connect to db server")
# create cursor
cur = con.cursor()
# display list of db on server
cur.execute("""SELECT datname from pg_database""")
rows = cur.fetchall()
# print list of db on server
print ("\nConnection successful. Listing databases on server:\n")
for row in rows:
print (" ", row[0])
# close the cursor
print ("\nClosing server connection.\n")
cur.close()
# close the connection
con.close()

In PostgreSQL's libpq (which psycopg2 is based on), if you specify a user name but not a dbname, it automatically tries to connect to the database which takes the same spelling as the user you specified.
You specify the user 'testuser', but don't specify a database, so it tries to connect to the database spelled 'testuser'. If that doesn't exist, of course the connection will fail.
You said the database which has to exist for this to work is "testdb", but I see that at one point you had specified the user name as "testdb" but then edited your question and changed it to "testuser". So I think the example you show is not the code you actual are running, as you did some kind of piecemeal editing of it after the fact.
In your connection code, you should specify a dbname which you know already exists.

You should really look at the gevent connection I wrote over here. It automates all of this for you and you don't have to reinvent the wheel.
Python Postgres psycopg2 ThreadedConnectionPool exhausted

Related

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 create a new DB or connect to existing one

I pulled and run neo4j docker:
sudo docker run -p7474:7474 -p7687:7687 -e NEO4J_AUTH=neo4j/s3cr3t neo4j
From python I can connect to it with:
scheme = "neo4j"
host_name = "localhost"
port = 7687
url = "{scheme}://{host_name}:{port}".format(scheme=scheme, host_name=host_name, port=port)
user = "neo4j"
password = "s3cr3t"
driver = GraphDatabase.driver(url, auth=(user, password))
But it seems that there is no API to choose the DB name I want to work with ?
Is it possible to create multiple databases (like postgres psycopg2 connect function with dbname ?)
I want to be able to create 2 different DBs (graphs) and to choose the DB (graph) to work with through python
How can I do it ?
To connect to a specific database, you can pass the database's name as the value of the database keyword argument when you create the Session used for your transactions.
For example, to create a Session for the database named "foo":
...
driver = GraphDatabase.driver(uri, auth=(user, password))
session = driver.session(database="foo")
...

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.

database "public" does not exist - postgres/sequelize

I'm trying to connect postgres database from sequelize (node.js).But sequelize throws error like ERROR: database "public" does not exist.
The database url is given below:
postgres://postgres:root#localhost:5432/public
The show db result is given below:
I have modified the database url as follows:
postgres://postgres:root#localhost:5432/postgres, where postgres is valid database.Please find the attached image below:
database and schema in postgresql are not the same object .. you should provide the database name not the schema name
you could get the list of the database using
psql -U pgadmin -l
The postgres database is a default database meant for use by users, utilities and third party applications.
If you don't specify a database while connecting to postgres (using, day, psql), it will connect to postgres DB.
It is possible that you connected to postgres without specifying the DB and created the tables. Hence they were created in postgres DB.
References:
https://www.postgresql.org/docs/15/app-initdb.html
https://stackoverflow.com/a/2411860/12242023

OperationalError: attempt to write a readonly database,

I am working on flask app and deployed to linux server when I am doing Insert Query its giving OperationalError: attempt to write a readonly database, in error log
import sqlite3 as sql
def insertUser(username,password,email,phone):
with sql.connect("/var/www/FlaskApp/database.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO user (username,password,email,phone) VALUES (?,?,?,?)", (username,password,email,phone))
con.commit()
It looks like the user running the flask application does not have permission to write to /var/www/FlaskApp/database.db. Have you checked your file permissions. It's perhaps best to have the file be owned by the user running your Flask application.

Resources