How to create a new DB or connect to existing one - python-3.x

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

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.

access postgresql server via python3 fails if (unrequested) db does not exist

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

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.

pyodbc cannot connect to on-premises MSSQL server

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)

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