Django app's DB user failed authentication in AWS - django

Here is Django setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'rulings',
'USER': 'postgres',
'PASSWORD': '******',
'HOST': '',
'PORT': '',
}
}
In AWS, when I run a server, an error message occurs.
File "/home/app_admin/venv_ruling/lib64/python3.7/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError:
FATAL: Peer authentication failed for user "postgres"
How can I run the server with the user name of postgres??
When I use another user name it will run without a problem, but I want to use "postgres" as a user.

I don't think "peer" authentication can even be configured on RDS, so you must be running your own PostgreSQL server on EC2? Since you apparently want to use password authentication, change your pg_hba to specify "md5", rather than "peer".
Also, it is a bad practise to run an app as "postgres". That user is supposed to be reserved for maintenance operations.

Related

connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "rootuser" does not exist

I am trying to connect deploy a django backend with postgres db on digitalocean droplet. But it's giving me this error after after gunicorn and nginx setup:
connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "rootuser" does not exist`
"rootuser" is my root username not the db username, my db username is dbadmin
I tried to create db user with name serveroot, it worked but started throwing other errors:
relation "django_site" does not exist LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...
Have a look at the docs about the settings file: https://docs.djangoproject.com/en/4.1/topics/settings/
Look for a section like this one:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': YOUR_DB_NAME,
'USER': USERNAME,
'PASSWORD': PASSWORD_FOR_DB,
'HOST': 'localhost' // in Development.
}
}
and then change the NAME, USER, PASSWORD and HOST values to the ones you need.

Django is trying to authenticate Wrong user for AWS RDS and failing

I am hosting a postgresql database on AWS using the RDS service. I am trying to connect the django project to the aws database using the settings.py file in the django project. I keep getting the following error:
connection to server at "database-1.xxxxxx.us-east-1.rds.amazonaws.com" (xx.xxx.xx.xxx), port 5432 failed: FATAL: password authentication failed for user "local_user"
This error is unexpected because it should not be trying to authenticate the local_user as this is the user for my local postgres server, it should be trying to authenticate the user for the hosted database, which is completely different.
This is what my settings file looks like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get('DATABASE_NAME'),
'USER': os.environ.get('DATABASE_USER'),
'PASSWORD': os.environ.get('DATABASE_PASSWORD'),
'HOST': "database-1.xxxxxx.us-east-1.rds.amazonaws.com",
'PORT': 5432
}
}
I can't figure out what the issue seems to be. Any help will be appreciated!
I named the environment variable wrong so USER was none!

PostgreSQL with Django failing to connect in Standar App Engine

I have a Django 4 app with PostgreSQL. I connected the SQL instance vie google-proxy correctly, however when I upload it into App Engine it fails to connect.
settings.py
if os.getenv('GAE_APPLICATION', None):
# Running on production App Engine, so connect to Google Cloud SQL using
# the unix socket at /cloudsql/<your-cloudsql-connection string>
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'HOST': '/cloudsql/instancename',
'USER': 'user',
'PASSWORD': '********',
'NAME': 'database',
'PORT': '5432',
}
}
else:
DATABASES = {
'default': {
"ENGINE": "django.db.backends.postgresql",
'NAME': 'database',
'USER': 'user',
'PASSWORD': '******',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
The error that on the screen was related with psycopg2:
could not connect to server: Connection refused
Is the server running locally and accepting
connections on Unix domain socket "/cloudsql/instancename/.s.PGSQL.5432"?
/layers/google.python.pip/pip/lib/python3.9/site-packages/psycopg2/__init__.py, line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
I updated the requirements.txt because locally with the proxy everything was correct.
asgiref==3.5.0
Django==4.0
django_better_admin_arrayfield==1.4.2
djangorestframework==3.13.1
django-cors-headers==3.7.0
django-filter==2.4.0
django-ratelimit==3.0.1
gunicorn==20.1.0
django-cors-headers==3.7.0
psycopg2==2.8.6
psycopg2-binary==2.9.3
pytz==2020.4
sqlparse==0.4.1
google-api-core==1.26.0
google-api-python-client==1.12.8
google-auth==1.27.0
google-auth-httplib2==0.0.4
google-cloud-core==1.6.0
google-cloud-storage==1.36.1
google-crc32c==1.1.2
google-resumable-media==1.2.0
googleapis-common-protos==1.53.0
I also added all the APIs necessaries according to the google docs
Lastly, I read the logs in App Engine and the first error is about IAM
rpc error: code = PermissionDenied desc = IAM permission denied for service account services#service.iam.gserviceaccount.com. "
I added all the possible permisions to that service account, however it continuous to happend.
I don't know what else to do.
Thanks you all

Connecting to a remote db through a jump server in Django

I'm trying to tunnel my db connection in a django application through a jump server but can't seem to get it working because django manage.py handles & process the connections.
here's that I have in the settings.py
#process ssh_key first
ssh_key= os.getenv('SSH_KEY', '').encode('utf8').decode('unicode_escape')
server ={}
with sshtunnel.open_tunnel(
(os.environ.get('SSH_HOST'),int(os.getenv('SSH_PORT'))),
ssh_pkey=paramiko.RSAKey.from_private_key(io.StringIO(ssh_key)),
ssh_username= os.environ.get('SSH_USERNAME'),
remote_bind_address=(os.environ.get('DB_HOST'), int(os.getenv('DB_PORT'))),
) as ssh_proxy_host:
server={
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': 'localhost',
'PORT': ssh_proxy_host.local_bind_port,
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASS'),
}
# here's where I should have the connection function to db, but don't know if django has that option available
The true remote host is specified upon tunnel creation. The HOST used for the db connection should be localhost because you need it to find your end of the tunnel, not the other end of it.

pythonanywhere: access denied for user 'USERNAME'#'%' to database

I am using www.pythonanywhere.com for deployment of my Django project. The pythonanywhere's database settings are as follows:
Connecting:
Database host address: mysql.server
Username: Username (just as an example)
Your databases:
Start a console on: Username$DBName (just as an example)
...
While setting up the database using "manage.py migrate" command, The error message showed up:
django.db.utils.OperationalError: (1044 "Access denied for user 'Username'#'%' to database 'DBName'")
DATABASES settings in file settings.py are as follows.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DBName',
'USER': 'Username',
'PASSWORD': 'password',
'HOST': 'mysql.server',
'PORT': '3306',
}
}
I am able to run mysql in the Console using the command:
$ mysql -u Username -h mysql.server -p
When I enter the following command:
mysql> use mysql;
I got the error message:
ERROR 1044 (42000): Access denied for user 'Username'#'%' to database 'mysql'
"show databases;" command shows there is no database named "mysql".
When I enter the following command:
mysql> GRANT ALL PRIVILEGES ON DBName.* TO 'Username'#'Username$DBName' IDENTIFIED BY 'password' WITH GRANT OPTION;
I also got the error message:
ERROR 1044 (42000): Access denied for user 'Username'#'%' to database 'DBName'
So, how do I setup the database?
Found this in the pythonanywhere documentation, the database settings to use MySQL should be of the form:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<your_username>$<your_database_name>',
'USER': '<your_username>',
'PASSWORD': '<your_mysql_password>',
'HOST': 'mysql.server',
}
}
If your username is say user and database is db, then your database name should be user$db, not db.
Similarly, to access the database in the console you type
mysql>use user$db;
and not
mysql>use db;