Way to identify cause of psycopg2 timeout - python

I am attempting to connect to a remote postgresql db in Python. I am using psycopg2. The following is the format used to establish a connection:
con = psycopg2.connect( dbname = config['db'],
host = config['host'],
port = config['port'],
user = config['user'],
password = config['pass'])
I have seen noted in other SO questions that connect_timeout = xx can be added to specify the timeout delay. Regardless, the issue is that when the timeout does occur it asks if host is correct and the port is, as well.
They are, and a similar connection set up in Node using JSFtp is successful. Are there any additional steps I can take to breaking down the steps psycopg2 is taking to identify what break is occurring?

Related

What does 'DPY-6001: cannot connect to database' mean with python-oracledb?

With Python code that uses the
python-oracledb driver:
import oracledb
import os
un = os.environ.get("PYTHON_USERNAME")
pw = os.environ.get("PYTHON_PASSWORD")
cs = "localhost/doesnotexist"
c = oracledb.connect(user=un, password=pw, dsn=cs)
what does this error message mean?
DPY-6001: cannot connect to database. Service "doesnotexist" is not registered with the listener at host "localhost" port 1521. (Similar to ORA-12514)
The error means that Python successfully reached a computer (in this case
"localhost" using the default port 1521) that is running a database. However
the database service you wanted ("doesnotexist") doesn't exist there.
Technically the error means the listener doesn't know about the service at the
moment. So you might also get this error if the DB is currently restarting.
This error is similar to the ORA-12514 error that you would see when connecting
with python-oracledb in Thick mode, or might see with some other Oracle tools.
The solution is to use a valid service name, for example:
cs = "localhost/xepdb1"
You can:
Check and fix any typos in the service name you used
Check the hostname and port are correct
Ask your DBA for the correct values
Wait a few moments and re-try in case the DB is restarting
Review the connection information in your cloud console or cloud wallet, if you are using a cloud DB
Run lsnrctl status on the database machine to find the known service names

Python - psycopg2 giving error after execution

I am getting this error when executing my code in Python.
Here is my Python - DataBaseHelper.py:
import psycopg2
#class
class DataBaseHelper:
database = "testdata";user = "test";password = "pass123"; host = "mtest.75tyey.us-east-1.rds.amazonaws.com"
#create and return the connection of database
def getConection(self):
self.conn = psycopg2.connect(database=self.database, user = self.user, password = self.password, host = self.host, port = "5432")
return self.conn
Then I am importing this file and using in another python file - MyScript.py:
import sys
import uuid
from DBHelper import DataBaseHelper
from ExecutionLogHelper import ExecutionLogHelper
from GotUtility import GotUtility
class MyScript:
def __init__(self,):
self.con = DataBaseHelper().getConection()
self.logHelper = ExecutionLogHelper()
self.uuid = self.logHelper.Get_TEST_Run_Id()
When I run my code concurrently, it gives me this error:
psycopg2.errors.AdminShutdown: terminating connection due to administrator command
SSL connection has been closed unexpectedly
I am not able to understand why am I getting this error. When I run the Python program again, it works. And I checked the Postgres server is in running, no restart, no signal to shutdown. This keeps on happening every few hours for me.
This is happening because psycopg2 is try to connect to AWS Postgresql over SSL and failing to do so.
Try connecting with sslmode = disable
def getConection(self):
self.conn = psycopg2.connect(database=self.database,
user = self.user,
password = self.password,
host = self.host,
port = "5432",
sslmode="disable")
return self.conn
Method 1 will not work if your AWS Postgresql is configured to force a ssl connection ie. parameter rds.force_ssl = 1. If you enable set rds.force_ssl all non-SSL connections are refused. In that case try connecting using something like this:
$ psql -h testpg.cdhmuqifdpib.us-east-1.rds.amazonaws.com -p 5432 "dbname=testpg user=testuser sslrootcert=rds-ca-2015-root.pem sslmode=verify-full"
For more on how to connect to AWS RDS over ssl using various drivers : AWS RDS SSL.
After a little digging, I found a few answers. According to this link:
This error message comes from intervention by a program external to
Postgres: http://www.postgresql.org/message-id/4564.1284559661#sss.pgh.pa.us
To elaborate, this link says:
If user stop postgresql server with "service postgresql stop" or if any SIGINT has been called on the postgresql PID then this error will occur.
Solution:
Since your code is running concurrently, multiple transactions at the same time, at the same row could be causing this error. So you've got to make sure that that doesn't happen... Look here for more details:
When you update rows in a transaction, these rows are locked until the transaction is committed.
If you are unable to do that, I suggest you enable query logging and look to see if something odd is in it.

Cannot connect to Oracle using python 3.4

I just started to learn python and try to connect to oracle 11g, but I always get following error
cx_Oracle.InternalError: No Oracle error?
Here is my simple script to connect to oracle
import cx_Oracle as oracle
con = oracle.connect('user/password#ip:port/service')
Already try to look for any reference in other sites including here but can't find the solution. I don't think I have connection issue to oracle, because I use the same PC to connect to oracle using PHP.
Any advise would be appreciated, thanks.
One thing to keep in mind anytime you work with Oracle is that they use a proprietary connection protocol TNS (Transparent Network Substrate).
Therefore, you might need to use the cx_Oracle.makedsn(ip, port, SID) method and then pass it to cx_Oracle.connect() method to create your connection. Thus the general format on how to set up Oracle connection is:
import cx_Oracle
ip = 'xxx.xxx.xx.xxx'
port = 'xxxx'
SID = 'SID'
username = 'username'
password = 'password'
dsn_tns = cx_Oracle.makedsn(ip, port, SID)
db = cx_Oracle.connect(username, password, dsn_tns)
This is assuming you have already gotten cx_Oracle to work and import properly, which can be finicky depending on your environment.

pymsql not connecting to a database

I am trying to make software that works on a large number of people's computers by connecting to a login server. I have set up a MSQL server using 24hosting and added a database. I then tried to access the database using python, but it gives me the error "No connection could be made because the target machine actively refused it".
I need everyone who downloads this program to be able to connect, not just this computer.
This is probably something to do with my server, and not to do with code, but I will post the code below anyway.
from os import getenv
import pymysql
server = getenv("31.220.17.13")
user = getenv("shutdow1_user")
password = getenv("DSAEWQ321")
conn = pymysql.connect(server, user , password, "tempdp")
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
cur.close()
conn.close()
Try to run tcpdump or tshark host 31.220.17.13 to see what exactly happens.
Most likely TCP connection to mysql port is filtered (you would see RST replies on SYN send), or if you see that connection is closed by remote party after TCP session is established - that would be a sign of remote mysql server not configured properly to accept client connection for your IP/username/password/database.

Problems in connecting to MusicBrainz database using psycopg2

I am trying to connect to the MusicBrainz database using the psycopg2 python's module. I have followed the instructions presented on http://musicbrainz.org/doc/MusicBrainz_Server/Setup, but I cannot succeed in connecting. In particular I am using the following little script:
import psycopg2
conn = psycopg2.connect( database = 'musicbrainz_db', user= 'musicbrainz', password = 'musicbrainz', port = 5000, host='10.16.65.250')
print "Connection Estabilished"
The problem is that when I launch it, it never reaches the print statement, and the console (I'm on linux) is block indefinitely. It does not even catches the ctrl-c kill, so I have to kill python itself in another console. What can cause this?
You seem to be mistaking MusicBrainz-Server to be only the database.
What's running on port 5000 is the Web Server.
You can access http://10.16.65.250:5000 in the browser.
Postgres is also running, but listens on localhost:5432.
This works:
import psycopg2
conn = psycopg2.connect(database="musicbrainz_db",
user="musicbrainz", password="musicbrainz",
port="5432", host="localhost")
print("Connection established")
In order to make postgres listen to more than localhost you need to change listen_addresses in /etc/postgresql/9.1/main/postgres.conf and make an entry for your (client) host or network in /etc/postgresql/9.1/main/pg_hba.conf.
My VM is running in a 192.168.1.0/24 network so I set listen_addresses='*' in postgres.conf and in pg_hab.conf:
host all all 192.168.1.0/24 trust
I can now connect from my local network to the DB in the VM.
Depending on what you actually need, you might not want to connect to the MusicBrainz Server via postgres. There is a MusicBrainz web service you can access in the VM.
Example:
http://10.16.65.250:5000/ws/2/artist/c5c2ea1c-4bde-4f4d-bd0b-47b200bf99d6.
In that case you might be interested in a library to process the data:
python-musicbrainzngs.
EDIT:
You need to set musicbrainzngs.set_hostname("10.16.65.250:5000") for musicbrainzngs to connect to your local VM.

Categories