I am new to python and postgresql. This is the first time i was trying to connect python to postgresql.But i am getting this error:
`Traceback (most recent call last):
File "/home/vishal/Courses/database/app.py", line 5, in <module>
user.save_to_db()
File "/home/vishal/Courses/database/user.py", line 13, in save_to_db
connection = psycopg2.connect(user='postgres', password='1234', database='learning', host='localhost')
File "/home/vishal/Courses/database/venv/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"`
Even i tried changing the password and creating new user with different password the result is same.
Where am i going wrong?
Related
I need to connect to Postgres DB via SSH tunnel. I use following code:
try:
with SSHTunnelForwarder(
(ssh_host, ssh_port),
ssh_username=ssh_user,
ssh_password=ssh_password,
remote_bind_address=(sql_hostname, 5432)) as server:
server.start()
print("server connected")
params = {
'database': sql_database,
'user': sql_username,
'password': sql_password,
'host': sql_hostname,
'port': server.local_bind_port
}
conn = psycopg2.connect(**params)
curs = conn.cursor()
print("database connected")
except:
print("Connection Failed")
As the result I get this warnings in the command line:
2023-02-09 23:09:16,285| ERROR | Password is required for key /Users/aleksandrlozko/.ssh/id_rsa
2023-02-09 23:09:16,286| ERROR | Password is required for key /Users/aleksandrlozko/.ssh/id_ed25519
Traceback (most recent call last):
File "/Users/aleksandrlozko/stages-customer-service-dashboard-atollhoding/modules/db/connect.py", line 73, in <module>
db = psql.PostgreSQLDB(
File "/Users/aleksandrlozko/stages-customer-service-dashboard-atollhoding/modules/db/psql.py", line 25, in __init__
self.pool = psycopg2.pool.SimpleConnectionPool(
File "/opt/anaconda3/envs/activity-dashboard-atollholding/lib/python3.10/site-packages/psycopg2/pool.py", line 59, in __init__
self._connect()
File "/opt/anaconda3/envs/activity-dashboard-atollholding/lib/python3.10/site-packages/psycopg2/pool.py", line 63, in _connect
conn = psycopg2.connect(*self._args, **self._kwargs)
File "/opt/anaconda3/envs/activity-dashboard-atollholding/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "172.26.201.14", port 5432 failed: Operation timed out
Is the server running on that host and accepting TCP/IP connections?
Through the pdAdmin it is possible to connect, but not using Python. What could be the problem? And at what stage am I making a mistake?
I'm building a Airflow server from the official docker image:
curl -LfO "https://airflow.apache.org/docs/apache-airflow/2.3.0/docker-compose.yaml"
It creates an standard user (airflow) with standard password (airflow). I have been trying to connect to my Airflow DAG with a PostgresHook, but it keeps returning this:
[2022-05-18, 01:29:32 UTC] {base.py:68} INFO - Using connection ID 'postgres_db' for task execution.
[2022-05-18, 01:29:32 UTC] {taskinstance.py:1889} ERROR - Task failed with exception
Traceback (most recent call last):
File "/home/airflow/.local/lib/python3.7/site-packages/airflow/operators/python.py", line 171, in execute
return_value = self.execute_callable()
File "/home/airflow/.local/lib/python3.7/site-packages/airflow/operators/python.py", line 189, in execute_callable
return self.python_callable(*self.op_args, **self.op_kwargs)
File "/opt/airflow/dags/etl_hover.py", line 35, in insert_data_to_table
pg_conn = hook.get_conn()
File "/home/airflow/.local/lib/python3.7/site-packages/airflow/providers/postgres/hooks/postgres.py", line 113, in get_conn
self.conn = psycopg2.connect(**conn_args)
File "/home/airflow/.local/lib/python3.7/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "host.docker.internal" (192.168.65.2), port 5432 failed: FATAL: password authentication failed for user "***"
And, this is the connection:
As you can see, It is pretty much a standard connection (I have created succesfully an entry point with the database api_db, by the way. But It keeps telling me something about a wrong password. Any ideas?
We are trying to access the database of Odoo running on 104.154.90.232 through Python. I have used psycopg2 to connect.
My code is like this:
import psycopg2
import sys
import pprint
conn_string = "host='104.154.90.232' dbname='dbname' user='user' password='password'"
print "connecting to the database\n ->%s"%(conn_string)
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute("SELECT * FROM hr_employee")
records = cursor.fetchall()
pprint.pprint(records)
The error is:
Traceback (most recent call last):
File "test.py", line 6, in <module>
conn = psycopg2.connect(conn_string)
File "C:\Python27\lib\site-packages\psycopg2\__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "104.154.90.232" and accepting TCP/IP connections on port 5432?
Can someone help me establish the connection?
As far as i know, the default pg_hba.conf file only allows connections resp. authentification from the local system. You have to define new rules for external access. It's described in the file, how to do that.
Then ofcourse there has to be a correctly configured database user for odoo.
I am trying to provision a server with a django applciation with postgresql as its backend. After installing the required packages, database and environment when I try to run migrations, I get the following error
Traceback (most recent call last):
File "/var/envs/traveldbapi/lib/python3.4/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/var/envs/traveldbapi/lib/python3.4/site-packages/django/db/backends/base/base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "/var/envs/traveldbapi/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
connection = Database.connect(**conn_params)
File "/var/envs/traveldbapi/lib/python3.4/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: fe_sendauth: no password supplied
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/var/envs/traveldbapi/lib/python3.4/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
====
ommitting some lines
====
connection = Database.connect(**conn_params)
File "/var/envs/traveldbapi/lib/python3.4/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: fe_sendauth: no password supplied
I confirmed that the relevant DATABASE setting required for django are present:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'HOST': 'localhost',
'NAME': DATABASE_NAME,
'USER': DATABASE_USER,
'password': DATABASE_PASSWORD
}
}
I am not sure why this error is happening because this is the same setup I use on my local machine and it works. To confirm that there aren't any issues with my pg_hba.conf I started from a fresh installation. The config hasn't been modified in any way and the application user has the required privileges on the application database.
The settings must be in uppercase - try changing it to 'PASSWORD'
The key name 'password' should be in uppercase 'PASSWORD'. Also instead of defining password as global variable DATABASE_PASSWORD, you can use .bashrc file to save secure information and can fetch in settings.py like os.environ['DATABASE_PASSWORD']
make your HOST variable empty as
'HOST': '',
OperationalError: fe_sendauth: no password supplied
you should change password to uppercase 'PASSWORD'
In using Django,ensure you have a .env file and configure properly your .postgres and .django envs esp provide Database URL in your .postres or in .env file as such:
DATABASE_URL=psql://postgres:yourpassword#127.0.0.1:5432/databasename
POSTGRES_HOST=*
POSTGRES_PORT=5432
POSTGRES_DB=tutor
POSTGRES_USER=postgres
POSTGRES_PASSWORD=yourpassword
DJANGO_ALLOWED_HOSTS=['.herokuapp.com', 'localhost', '127.0.0.1', '[::1]', '0.0.0.0']
# DJANGO_DEBUG=False # disable debugging on production
DJANGO_DEBUG=False # disable debugging if hosts list is not empty
For more info on environmental variables please refer to: # .env using django-environ
I am trying to connect Python to a web server's database, 000webhost I am using. Here is my code:
import MySQLdb
>>> conn = MySQLdb.connect(host="mysql9.000webhost.com",user="user_name",passwd="password",db="db_name")
I think I install MySQLdb successfully. And all the username, password, host, database name are exactly the same as they are in my php file, which works perfectly. However, I got this error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
conn = MySQLdb.connect(host="mysql9.000webhost.com",user="a6969519_123",passwd="lianshiyu08",db="a6969519_shiyu")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'mysql9.000webhost.com' (60)")
What did I miss?
The database is only reachable from the internal web of your hoster.