I want to connect to a remote Postgresql (installed on raspberry pi) DB in Python. I have followed the example from the link below:
https://opensource.com/article/17/10/set-postgres-database-your-raspberry-pi
using the following code:
conn = psycopg2.connect('host=192.xxx.x.x user=pi password = raspberry dbname = test')
Not sure what goes wrong. Anybody a solution.
Unfortunately I get the following error:
psycopg2.OperationalError: FATAL: password authentication failed for user "pi"
FATAL: password authentication failed for user "pi"
I strongly recommend supplying the credentials as keyword arguments for better readability:
psycopg2.connect(
user="...",
password="...",
dbname="...",
host="...",
)
Also, as #peterh mentioned in the comments, your provided credentials might be parsed incorrectly (not in this case though). You can use psycopg2's own parser to confirm the output:
psycopg2._psycopg.parse_dsn('host=192.xxx.x.x user=pi password = raspberry dbname = test')
Which returns:
{'user': 'pi',
'password': 'raspberry',
'dbname': 'test',
'host': '192.xxx.x.x'}
All things considered, your error suggests that the User/Password combination is incorrect.
Related
I am trying to connect to an oracle database in Python using create_engine. This database does not have a username or password.
I see this is the format now:
oracle_db = sqlalchemy.create_engine('oracle://user:pass#server').
However, if this connection has NO username or password, how would the connection string look? I've tried DMIT_connection = create_engine('oracle+cx_oracle://#....) with no luck. When I go to write a pandas df to the database using to_sql I get the error below because I cannot get the connection right give that there is no username or password.
The error occurs because this database has no username (picked up from the localhost machine) and there is no password.
The error I get is this: DatabaseError: (cx_Oracle.DatabaseError) ORA-12545: Connect failed because target host or object does not exist (Background on this error at: http://sqlalche.me/e/14/4xp6)
Let me know authentication type used. If its external authentication , picking credentials from wallet, you can try sample code mentioned here
How to use SqlAlchemy to connect Database similar to cx_oracle when we use external authorization like wallets with TNS(net service name)
My goal is to use getpass to hide the entry of my password when I connect to a postgresql database via python3.
I use python3 on jyputer notebook.
This work well :
connect = psycopg2.connect("dbname='db_toto' user='dad' host='xx.xx.xxx.x' port='5432' password='123456'")
cur = connect.cursor()
But when I try to enter the password with a separate variable, it does not work anymore :
pw = getpass.getpass()
####Python ask me to tape password and i tape the same '123456'
To verify :
'123456'
connect=psycopg2.connect("dbname='db_toto' user='dad' host='xx.xx.xxx.x' port='5432' password=pw")
cur=connect.cursor()
" OperationalError: FATAL: password authentication failed for user
FATAL: password authentication failed for user "
received error message
Thanks you for your help
What you're doing is passing a string to the connect function. This string has the value of "dbname='db_toto' user='dad' host='xx.xx.xxx.x' port='5432' password=pw". The psycopg2 module has no way of knowing what pw is. I suspect it will be converted to a string ('pw'), but I'm not sure.
Anyway, the correct approach would be to pass keyword arguments to the connect function like so:
connect = psycopg2.connect(dbname='db_toto' user='dad' host='xx.xx.xxx.x' port='5432' password=pw)
# Notice the lack of double-quotes in the arguments
This way, you will be passing the contents of the pw variable to the function, instead of the name pw.
It is possible to pass the contents of the pw variable in string form as well, like so:
connect = psycopg2.connect("dbname='db_toto' user='dad' host='xx.xx.xxx.x' port='5432' password='{}'".format(pw))
The first form should be preferred.
I have a MySQL Server set up to use SSL and I also have the CA Certificate.
When I connect to the server using MySQL Workbench, I do not need the certificate. I can also connect to the server using Python and MySQLdb on a Mac without the CA-certificate.
But when I try to connect using the exact same setup of Python and MySQLdb on a windows machine, I get access denied. It appears that I need the CA. And when I enter the CA, I get the following error
_mysql_exceptions.OperationalError: (2026, 'SSL connection error')
My code to open the connection is below:
db = MySQLdb.connect(host="host.name",
port=3306,
user="user",
passwd="secret_password",
db="database",
ssl={'ca': '/path/to/ca/cert'})
Could anyone point out what the problem is on a windows?
I just got the following to work with Python 2.7 and MySQLdb (1.2.4):
database = MySQLdb.connect(host='hostname', user='username', db='db_name',
passwd='PASSWORD', ssl={'ca': '/path/to/ca-file'})
This is what you had so there must be something else going on here. I wonder if you have something either incorrect with the your local CA file or possibly the cert on the server? Can you get a copy of the CA file from the server?
Try this
import ssl
from databases import Database
sslctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH,
cafile='ca.pem')
sslctx.verify_mode = ssl.CERT_REQUIRED
sslctx.check_hostname = True
sslctx.load_cert_chain(certfile='client.crt', keyfile='pkey.key')
database = Database(DATABASE_URL, ssl=sslctx)
databases library provides support for PostgreSQL, MySQL, and SQLite.
Also useful for async frameworks.
Im using pymysql and I had some problems to connect using SSL keys and certs: for the "ssl" attribute I set up as a dictionary inside. Try as below:
db = MySQLdb.connect(host="host.name",
port=3306,
user="user",
passwd="secret_password",
db="database",
ssl={'ssl':
{'ca': '/path/to/ca/ca',
'key': '/path/to/ca/key',
'cert': '/path/to/ca/cert'
}
}
)
I know this is a bit old but I found a way to get this to work. Use pymysql instead of MySQLdb and write the connection as:
import pymysql
conn = pymysql.connect(user = 'user', password = 'passwd'
, database = 'db', host = 'hst', ssl = {'ssl' : {'ca': 'pathtosll/something.pem'}})
The point people miss (including myself) is that ssl needs to be a dictionary containing
a key 'ssl' which has another dictionary as a value with a key 'ca'. This should work for you.
import pymysql
conn = pymysql.connect(host= # your host, usually localhost,
user = # your username,
passwd = # your password,
db = #your database name ,
ssl ={'ssl': r'path of your pem file'})
I'm connecting Hive use pyhs2. But the Hive server required Kerberos authentication. Anyone knows how to convert the JDBC string to pyhs2 parameter? Like:
jdbc:hive2://biclient2.server.163.org:10000/default;principal=hive/app-20.photo.163.org#HADOOP.HZ.NETEASE.COM?mapred.job.queue.name=default
I think it will be something like this:
pyhs2.connect(host='biclient2.server.163.org',
port=10000,
authMechanism="KERBEROS",
password="something",
user='your_user#HADOOP.HZ.NETEASE.COM')
I'm also doing the same, I still not succeed, but at least having a meaningful errorcode:
(Server hive/xxx#yyy.COM not found in Kerberos database)
This connection string will work as long as the user running the script has a valid kerberos ticket:
import pyhs2
with pyhs2.connect(host='biclient2.server.163.org',
port=10000,
authMechanism="KERBEROS") as conn:
with conn.cursor() as cur:
print cur.getDatabases()
Username, password and any other configuration parameters are not
passed through the KDC.
i'm a newbie to python, trying to learn some stuff by writing server scripts which connect to our mysql databases and do some stuff. i have python 3.3.1, and mysql 5.0.96 (community). i installed the mysql connector/python, and tried to do a basic connection using the sample code on the mysql dev site. here is my simple python script:
#!/usr/local/bin/python3
import sys
import mysql.connector
db_config = {
'user': 'joebloggs',
'password': 'cleartext_passwd',
'host': '127.0.0.1',
'database': 'mydb'
}
cnx = mysql.connector.connect(**db_config)
cursor = cnx.cursor()
query = ('select usable_name, user_id from user')
cursor.execute(query)
for (usable_name, user_id) in cursor:
print("{} is the usable name of {d}".format(usable_name, user_id))
cursor.close()
cnx.close()
but i am getting an error connecting to the database
"Authentication with old (insecure) passwords "\
mysql.connector.errors.NotSupportedError: Authentication with old (insecure) passwords is not supported. For more information, lookup Password Hashing in the latest MySQL manual
what am i doing wrong? any help greatly appreciated
MySQL supports two password encryption schemes, and yours are using the old legacy version. Newer clients refuse to use those as they're pretty trivial to crack.
You need to update your passwords to the new style: http://dev.mysql.com/doc/refman/4.1/en/old-client.html