I've been given a postgres DB in my uni project in which I have to SSH into the network from which I can then access the DB. I've set up the connection in DBeaver using the SSH tab and it works perfectly fine. However, using Python, I can connect to the SSH just fine, but cannot connect to the DB itself. I've checked with another DB that doesn't require SSH and that works just fine. Here is my code. Note: I've already tried using SSHTunnel, too, to no avail. Also, ignore my quick hack to anonymize my SSH login data, as I didn't find how to use a proper config file with paramiko late at night yesterday...
import os
from psycopg2 import connect, Error
from paramiko import SSHClient
from config import config
with open("ssh_config.txt", "r") as f:
lines = f.readlines()
hostname = lines[0].strip()
username = lines[1].strip()
password = lines[2].strip()
ssh = SSHClient()
ssh.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
ssh.connect(hostname=hostname, username=username, password=password)
print("SSH connected.")
try:
params = config()
conn = connect(**params)
cursor = conn.cursor()
print("DB connected.")
# Print PostgreSQL connection properties.
print(conn.get_dsn_parameters(), "\n")
# Print PostgreSQL version.
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record, "\n")
except (Exception, Error) as error:
print("Error while connecting to PostgreSQL", error)
Any help would be appreciated. Thanks!
I've figured it out myself. Here is the updated code. Basically, I had to forward the remote address to localhost and then connect to localhost instead of the DB address.
from psycopg2 import connect, Error
from sshtunnel import SSHTunnelForwarder
from config import config
with open("ssh_config.txt", "r") as f:
lines = f.readlines()
hostname = lines[0].strip()
username = lines[1].strip()
password = lines[2].strip()
remote_bind_address = lines[3].strip()
try:
with SSHTunnelForwarder(
(hostname, 22),
ssh_username=username,
ssh_password=password,
remote_bind_address=(remote_bind_address, 5432),
local_bind_address=("localhost", 8080)) \
as tunnel:
tunnel.start()
print("SSH connected.")
params = config()
conn = connect(**params)
cursor = conn.cursor()
print("DB connected.")
# Print PostgreSQL connection properties.
print(conn.get_dsn_parameters(), "\n")
# Print PostgreSQL version.
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record, "\n")
cursor.close()
conn.close()
tunnel.close()
print("DB disconnected.")
except (Exception, Error) as error:
print("Error while connecting to DB", error)
Related
I am new in using python and I want to try first connecting to mongodb database using python
This is my code but is has error
import pymongo
DATABASE_NAME = "testdb"
DATABASE_HOST = "mongodb://10.10.100.100:22222"
DATABASE_USERNAME = "testuserid"
DATABASE_PASSWORD = "testpass"
try:
myclient = pymongo.MongoClient(DATABASE_HOST)
myclient.test.authenticate( DATABASE_USERNAME , DATABASE_PASSWORD )
mydb = myclient[DATABASE_NAME]
print("[+] Database connected!")
except Exception as e:
print("[+] Database connection error!")
raise e
10.10.100.100 is just my sample address and also 22222 is sample port
DATABASE_URI = f"mongodb://{DATABASE_USERNAME}:{DATABASE_PASSWORD}#{DATABASE_HOST}"
myclient = pymongo.MongoClient(DATABASE_URI)
I am new to Database operations and i am writing a python script to take backup and restore the backup of an db in postgres
Below is my python script
import subprocess
import psycopg2
user = "postgres"
password = "postgres"
host = "localhost"
port = "5432"
database_name = "test"
dest_file = "/home/admin/temp/db.sql"
#Taking db backup
process = subprocess.Popen(['pg_dump','--dbname=postgresql://{}:{}#{}:{}/{}'.format(user, password, host, port, database_name),'-Fc','-f', dest_file],stdout=subprocess.PIPE)
output = process.communicate()[0]
if process.returncode != 0:
print('Command failed. Return code : {}'.format(process.returncode))
exit(1)
print(str(process))
#Doing db changes
#Restoring db in a chance of error
conn = psycopg2.connect(user = user,password = password,host = host,port = port)
conn.autocommit = True
with conn.cursor() as cursor:
cursor.execute('DROP DATABASE test;')
cursor.execute('CREATE DATABASE test;')
process = subprocess.Popen(['pg_restore', '--no-owner','--dbname=postgresql://{}:{}#{}:{}/{}'.format(user, password, host, port, database_name), dest_file],stdout=subprocess.PIPE)
output = process.communicate()[0]
if process.returncode != 0:
print('Command failed. Return code : {}'.format(process.returncode))
exit(1)
print(output)
While executing code i am getting the follwing error..
psycopg2.errors.ObjectInUse: database "test" is being accessed by other users
Not sure whats wrong..
Please help
You have connections on your test database which must be closed before proceeding which can be achieved using the following:
-- Stop further connections
alter database test allow_connections = off;
-- Drop remaining (except this connection)
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'test'
AND pid <> pg_backend_pid();
The database can now be dropped - either via your code or SQL i.e.
drop database test;
I have database in MySQL workbench and I'm running Python code from shell to connect to my database.
But I am unable to import mysql.connector or mysqlDb (got this suggestion from w3school). So I am unable to connect to my database.
I think the question does not concern connecting with Putty, but more about importing mysql.connector in your python script.
"But I am unable to import mysql.connector or mysqlDb ( got this suggestion form w3school)"
--It will be easier to help you if your code is attached to your post.
import mysql.connector
cnx = mysql.connector.connect(########)
think this scrap of code will be helpful."Loading file from Oracle"
def load_file(DT):
ip = ######
port = #####
service_name = '#####'
dsn = cx_Oracle.makedsn(ip, port, service_name=service_name)
t = datetime.now().strftime("%Y%m%d%H%M%S")
SQL = "select name...."
fn = "/root/..." + t + ".csv"
fn_tmp = "/root/.." + t + ".csv"
FILE = open(fn_tmp, "w")
FILE.write(
'name...;' + '\n')
out = csv.writer(FILE, delimiter=';')
logging.info("Database connect " + t)
try:
conn = cx_Oracle.connect('####', '###', dsn)
cur = conn.cursor()
cur.execute(SQL)
except cx_Oracle.DatabaseError, exc:
conn = None
error, = exc.args
logging.error(
"Oracle connection problem Error Code:{0}".format(str(error).decode("####",
"ignore").encode("utf-8",
"ignore"))
)
for row in cur:
out.writerow(localize_floats(row))
logging.info("Database disconnect " + t)
try:
cur.close()
conn.close()
except cx_Oracle.DatabaseError, exc:
error, = exc.argsgv[0]
logging.error("Oracle-Error-Code:{0}".format(str(error).decode("####",
"ignore").encode("utf-8",
"ignore"))
)
FILE.close()
os.rename(fn_tmp, fn)
An option that you can try is to use pymysql. More info here. A code snippet is the following:
import pymysql.cursors
connection = pymysql.connect(host='IP',
port=3306,
user='user',
password='pass',
db='database',
charset='utf8mb4',
autocommit=True,
cursorclass=pymysql.cursors.DictCursor)
with connection.cursor() as cursor:
query = 'Your query'
cursor.execute(query)
connection.close()
I want to access a remote database through SSH tunnel.
server = SSHTunnelForwarder(
('172.17.9.125', 22),
ssh_password="123456",
ssh_username="root",
remote_bind_address=('127.0.0.1', 3306))
server.start()
database = pymysql.connect(host='127.0.0.1',
port=3306,
user='root',
passwd='root')
try:
dbsql = "CREATE DATABASE TestDB" # Create database
except Exception as e:
print("Error: ", e)
database.cursor().execute(dbsql)
global db, cursor
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='root', db='TestDB')
cursor = db.cursor()
print("Connected to MySQL database")
f = open("testdb.sql") # Execute .sql file, creating data tables
full_sql = f.read()
sql_commands = full_sql.split(';')[:-1]
try:
for sql_command in sql_commands:
if sql_command is not None:
cursor.execute(sql_command)
else:
print("Null")
print("Created database")
except Exception as e:
print("Error: ", e)
I want to create a new database named as "TESTDB" on this remote server, 172.17.9.125.
But I get this database created in localhost. What am I doing wrong here?
Since you already have a database running, your 3306 port is used in that so won't be able to bind.
Solution : bind it to some other port and try to connect to that.
You can bind to another address using local_bind_address=('0.0.0.0', 1234) (this will be your target local address/port to which it will be bound) in your arguments to SSHTunnelForwarder.
So your connection TunnelForwarder should be something like this
server = SSHTunnelForwarder(
('172.17.9.125', 22),
ssh_password="123456",
ssh_username="root",
local_bind_address=('0.0.0.0', 1234),
remote_bind_address=('127.0.0.1', 3306))
And now connection will be made to port 1234
database = pymysql.connect(host='127.0.0.1',
port=1234,
user='root',
passwd='root')
db = pymysql.connect(host='127.0.0.1', port=1234, user='root', passwd='root', db='TestDB')
I'm attempting to connect a database, located on a web server, to a robot but I do not know how to connect the database to the robot. I would like the robot to run SELECT and UPDATE queries from the robot. The other issue is that I do not intend on using C-languages or Java; I plan on using python in the main control system.
I do know:
PHP
VBScript
Batch
Python
If anyone knows how to connect the DB to a bot it would be a great help.
So basically how to connect to an SQL DB in python? I'm working on a virtual bot right now doing the same thing. Look into the module , SQL-connector! http://www.mysqltutorial.org/python-connecting-mysql-databases/
You would start with creating a config.ini with your credentials
[mysql]
host = localhost
database = python_mysql
user = root
password =
Read Config.ini and return a dictionary
from configparser import ConfigParser
def read_db_config(filename='config.ini', section='mysql'):
""" Read database configuration file and return a dictionary object
:param filename: name of the configuration file
:param section: section of database configuration
:return: a dictionary of database parameters
"""
# create parser and read ini configuration file
parser = ConfigParser()
parser.read(filename)
# get section, default to mysql
db = {}
if parser.has_section(section):
items = parser.items(section)
for item in items:
db[item[0]] = item[1]
else:
raise Exception('{0} not found in the {1} file'.format(section, filename))
return db
and connect to MYSQL database
from mysql.connector import MySQLConnection, Error
from python_mysql_dbconfig import read_db_config
def connect():
""" Connect to MySQL database """
db_config = read_db_config()
try:
print('Connecting to MySQL database...')
conn = MySQLConnection(**db_config)
if conn.is_connected():
print('connection established.')
else:
print('connection failed.')
except Error as error:
print(error)
finally:
conn.close()
print('Connection closed.')
if __name__ == '__main__':
connect()
and update statement would look like the following
def update_book(book_id, title):
# read database configuration
db_config = read_db_config()
# prepare query and data
query = """ UPDATE books
SET title = %s
WHERE id = %s """
data = (title, book_id)
try:
conn = MySQLConnection(**db_config)
# update book title
cursor = conn.cursor()
cursor.execute(query, data)
# accept the changes
conn.commit()
except Error as error:
print(error)
finally:
cursor.close()
conn.close()
if __name__ == '__main__':
update_book(37, 'The Giant on the Hill *** TEST ***')