Error connection to postgres database using psycopg2 - python

I am trying to connect to a postgres database to run some queries for validation. Trying to establish connection via psycopg2. Have tried passing actual values for host instead of variables.
Tried the connection using psql to verify the connection parameters, password etc and they are right too.
But with the code below it always errors out with the below error
conn1 = psycopg2.connect(host=db_host,database=db_instance_name,user=masterusername,port=db_port,password=masterpassword)
cur1 = conn1.cursor()
print ('\n************Connection is successful ************ \n')
Traceback (most recent call last):
File "restored_db_details_draft.py", line 50, in <module>
conn_test = psycopg2.connect(host="***",database="my_db_name",user="master",port="9876",password="****")
File "/Users/me/Library/Python/3.8/lib/python/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.

Related

How to connect fdb database using Python in Ubuntu?

I am trying to connect fdb database using python. I am remotely connected to ubuntu. I have tried this:
path = '/home/ubuntu/Firebird4.0/A.fdb'
con = fdb.connect(host=host, database=path, user=user, password=pswd2, charset='UTF8')
and many combinations for the path of ubuntu fdb file. How can I solve this problem? Is it about the wrong host, port, password, or something?
I got this error all the time
Traceback (most recent call last):
File "/home/ubuntu/test.py", line 24, in <module>
conn = connection()
File "/home/ubuntu/test.py", line 15, in connection
con = fdb.connect(host=host, database=path, user=user, password=pswd2, charset='UTF8')
File "/home/ubuntu/.local/lib/python3.10/site-packages/fdb/fbcore.py", line 869, in connect
raise exception_from_status(DatabaseError, _isc_status,
fdb.fbcore.DatabaseError: ('Error while connecting to database:\n- SQLCODE: -902\n- Unable to complete network request to host "15.206.100.218".\n- Failed to establish a connection.', -902, 335544721)

Cannot Connect To RDS Instance Using Python pymysql Library

I am attempting to connect to an RDS instance using Python's pymysql library. Unfortunately I'm getting an exception back.
Here is my Python code:
import pymysql
#Connect
connection = pymysql.connect(host='jdbc:mysql://dbname.url.us-east-1.rds.amazonaws.com/dbname',
port='3306',
user='username',
password='pass',
database='dbname')
Upon executing it, I receive the following exception:
Traceback (most recent call last):
File "/path/to/file/test.py", line 8, in <module>
database='treemigodb')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymysql/connections.py", line 327, in __init__
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymysql/connections.py", line 629, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'jdbc:mysql://dbname.url.us-east-1.rds.amazonaws.com/dbname' ([Errno 8] nodename nor servname provided, or not known)")
Here is what I've tried:
I've opened up the security group on the AWS side to traffic. I had previously performed this connection in Java, with success. AWS is configured as it should be.
I've seen in other examples that the host format tends to vary. Therefore, I've tried:
jdbc:mysql://dbname.url.us-east-1.rds.amazonaws.com/dbname
jdbc:mysql://url.us-east-1.rds.amazonaws.com/dbname
jdbc:mysql://dbname.url.us-east-1.rds.amazonaws.com/
jdbc:mysql://url.us-east-1.rds.amazonaws.com/
dbname.url.us-east-1.rds.amazonaws.com/dbname
url.us-east-1.rds.amazonaws.com/dbname
dbname.url.us-east-1.rds.amazonaws.com/
Is there some potential setup that I could be missing? I've double-checked that every other parameter matches.

An error with psycopg2 while trying to connect to a remote host server

I'm writing a script that should connect to postgres DB. My code is as follows:
import psycopg2
conn = connect(host="192.168.89.121")
But this is generating the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: FATAL: no hay una l�nea en pg_hba.conf para �192.168.89.3�, usuario �fgc�, base de datos �fgc�, SSL inactivo
Why is it asking for an entry for 192.168.89.3 which is my IP?
Do you have access to the remote PostgreSQL server? The error message appears to be trying to tell you that the configuration of that PostgreSQL server isn't allowing you to make an insecure connection. Look at the pg_hba.conf file for the database on that PostgreSQL server. It determines how different machines can connect to it. That's why it's mentioning your IP -- different rules can exist for different IP addresses.

Error 2003 Can't connect to MySQL server on 'localhost' PyMySQL

I'm not really sure what to do here. I'm trying to connect to MySQL Community Server by using a python script, and I'm running into an error message. This is being run on OS X Yosemite. I'm using the PyMySQL module, which imports correctly. The line
con = pms.connect(host='localhost',user='jaxon',passwd='password')
throws the following error message:
Traceback (most recent call last):
File "/Users/icefreak21/Documents/Python Files/DBConnector.py", line 4, in <module>
con = pms.connect(host='127.0.0.1',user='root',passwd='c0c0_puFF')
File "/Users/icefreak21/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymysql/__init__.py", line 88, in Connect
return Connection(*args, **kwargs)
File "/Users/icefreak21/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymysql/connections.py", line 644, in __init__
self._connect()
File "/Users/icefreak21/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/pymysql/connections.py", line 869, in _connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 61] Connection refused)")
I've searched around on the site and haven't found a question that details this particular situation. As far as I can tell, the server is running; the kicker is that I have no problem connecting through the command line with the same host, user, and password. I've tried switching the host name to '127.0.0.1' and I've tried specifying the port number (3306).
I found one solution that suggested specifying the socket, but when I ran the suggested code
./mysqladmin variables | grep socket
to find the socket file to specify, I get the following error code:
./mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'jaxon'#'localhost' (using password: NO)'
Does anyone have any other suggestions?
I had the same problem. I solved it by changing the port. You can add your port like this:
3307(port = 3307)
I'm recommending this because the mysql port of mac vision might be 3307.

'Interface error: 2003' when connecting to database

I'm trying to use MySQL Connection/Python to connect to my database.
Here's the output I'm getting:
Traceback (most recent call last):
File "bh2000.py", line 33, in <module>
cnx = mysql.connector.connect(**config)
File "/Library/Python/2.7/site-packages/mysql/connector/__init__.py", line 155, in connect
return MySQLConnection(*args, **kwargs)
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 123, in __init__
self.connect(**kwargs)
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 430, in connect
self._open_connection()
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 393, in _open_connection
self._socket.open_connection()
File "/Library/Python/2.7/site-packages/mysql/connector/network.py", line 375, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'host xxx.db.1and1.com:3306' (8 nodename nor servname provided, or not known)
Here's the code I'm using:
import mysql.connector
cnx = mysql.connector.connect(user='xxx', password='xxx',
host='xxx.db.1and1.com',
database='xxx')
cnx.close()
Where am I going wrong?
Did you specify the correct port?
Is your MySQL server running?
Is a firewall blocking access?
Try removing anonymous user account from your MySQL server?
Default port if not specified is 3306. Otherwise there is nothing wrong with your code. The problem is with your MySQL server or the connection is being blocked by your firewall or the server firewall. Make sure port 3306 is open and not blocked.
db = mysql.connector.connect(user='xxx', password='xxx', host='xxx.db.1and1.com', port=3306)
I believe the answer is because the hosting service you use, 1and1.com, uses a firewall that blocks all outside access to their databases. You have to run the code from a url that they host or it will not work.
I use the same server and had to figure this out the hard way myself. Below is a copy of what they have on one of their pages:
Access to the database via your website/presence only Please always
establish the connection to your database via your website/presence.
For security reasons, it is not possible to access the database
directly, for example via your local computer (external ODBC
connection).
To protect your data, your MySQL database is located on a dedicated
database server that is protected by a firewall.
Add a port argument in the command.
cnx = mysql.connector.connect(user='myuser',
password='mypassword', host='localhost', port='3306', database='mydb')
I got the same error. Once crosscheck the details you have entered. The host must be in the format 127.0.0.1. There should not be any / after the url.
Use localhost instead of IP, if you are working on same server.
myConnection = mysql.connector.connect(user='uname', password='pass', host='localhost', port='3306', database='databasename')

Categories