Django-postgresql: psycopg2 dsn takes wrong password - python

I am running python 2.7.3 and django 1.5.1 with postgresql DB. After setting up a simple django app I am getting a OperationalError at FATAL: password authentication failed for user "postgres". When I checked the local variables I found that psycopg2 connection is taking in the wrong password. I had specified the correct password in the django settings.py file.
Also manage.py syncdb works fine, and this problem shows up 2 out 3 times.
All authentication methods in pg_hba.conf is set to md5 and settings.py has HOST: localhost

Don't use the user postgres to do that.
Create a user and a db for your app, and use that user only for that.
su
su postgres
createuser -P <dbusername>
:type and re-type <dbuserpasswd>
createdb -O <dbusername> <dbname>
Check your pg_hba.conf
local all all md5
Restart your postgresql server.
Check your Django Settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'dbname', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'dbusername',
'PASSWORD': 'dbuserpasswd',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
Restart your http server.

Related

How to run an existing Django application on aws ec2 instance?

I am trying to run a Django application on AWS Ec2 instance. I've chosen Ubuntu as my platform. After cloning the git repository, and creating a virtual environment, I have installed all apps in my requirements.txt. When I try to the following lines of code python3 manage.py migrate ; python3 manage.py check ; python3 manage.py runserver the following error is coming up.
django.db.utils.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "columbus_db" connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "columbus_db"
My settings.py file looks like this
DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Database Engine of PostgreSQL Database
'NAME': 'columbus_db', # Database Name
'USER': 'columbus_db', # Database has a Root User
'PASSWORD': 'columbus', # Database Connection Password
'HOST': "localhost", # IP Address for Localhost
}
What can I change in settings.py or Ec2 Instance settings to start the application and see it at Ec2 IP address?
You are missing a running database, the app code except it to be PostgreSQL, you have multiple choices:
Install and run a local PostgreSQL instance directly in your EC2
Use Amazon's managed database RDS
Use Sqlite which is simple to setup and doesn't require more configuration, but your app might required specific PostgreSQL features

Cannot connect to postgresql databse from django settings.py on localhost

Forgive me as I this is my first django project using postgresql (version 11.8). For now I just want to connect to a test database which I have set up locally using pgadmin4. When I create the database I am not given the option to add a password, but when I run python manage.py migrate it is insisting on a password. When I then set the password to "password" on pgadmin, django won't accept it. It's probably something really obvious as I am quite new to Django, but I have tried searching and not found the answer to my problem. In settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test1',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Last line of the error when I run python manage.py migrate:
django.db.utils.OperationalError: fe_sendauth: no password supplied
Any help much appreciated. Craig
When you installed PgAdmin4 it asked you to create a password. This is for the superuser postgres, which is what you are trying to connect as. Use psql to connect to the database using the above settings and supply the password and see if it works.
I resolved it by removing the Postgresql 11 server and reinstalling it.

Connect MySQL Workbench with Django in Eclipse in a mac

I am new to this so a silly question
I am trying to make a demo website using Django for that I need a database.. Have downloaded and installed MySQL Workbench for the same. But I don't know how to setup this.
Thank you in advance :)
I tried googling stuff but didn't find any exact solution for the same.
Please help
I am a mac user. I have luckily overcome the issue with connecting Django to mysql workbench. I assume that you have already installed Django package created your project directory e.g. mysite.
Initially after installation of MySQL workbench i have created a database : create database djo;
Go to mysite/settings.py and edit following piece of block.
NOTE: Keep Engine name "django.db.backends.mysql" while using MySQL server.
and STOP the other Django MySQL service which might be running.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'djo', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': '****', # Replace **** with your set password.
'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '3306', # Set to empty string for default.
}
}
now run manage.py to sync your database :
$ python mysite/manage.py syncdb
bash-3.2$ python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'ambershe'): root
Email address: ambershe#netapp.com
/Users/ambershe/Library/Containers/com.bitnami.django/Data/app/python/lib/python2.7/getpass.py:83: GetPassWarning: Can not control echo on the terminal.
passwd = fallback_getpass(prompt, stream)
Warning: Password input may be echoed.
Password: ****
Warning: Password input may be echoed.
Password (again): ****
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

Setting up Django to connect to local SQLExpress in windows

I have successfully installed and setup Django in windows. I have also created my first project using django default database setting.
However, I would like to change the DATABASE settings to connect to my local SQLExpress.
Here is my DATABASE settings in settings.py file:
DATABASES = {
'default': {
#'ENGINE': 'django.db.backends.sqlite3',
#'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'NAME': 'mytestdb',
'ENGINE': 'sqlserver_ado',
'HOST': 'localhost',
'USER': 'test_user',
'PASSWORD': '1234',
}
}
After I saved the file, I ran into a problem where it says "Could not open a connection to SQL Server [2]".
I am able to connect to my local SQLExpress server using SQL Server Management studio. I know that the server is running. I've tried to google it and tried different setups, and I couldn't get it running.
This is the error that I've got when I ran syncdb command:
python manage.py syncdb
django.db.utils.OperationalError: (com_error(-2147352567, 'Exception occurred.', (0, u'Microsoft SQL Server Native Clien
t 10.0', u'Named Pipes Provider: Could not open a connection to SQL Server [2]. ', None, 0, -2147467259), None), u'Error
opening connection: DATA SOURCE=localhost;Initial Catalog=mytestdb;UID=test_user;PWD=******;PROVIDER=sqlncli10;DataTy
peCompatibility=80;MARS Connection=True')
Here is my installed packages:
C:\windows\system32>pip freeze
Django==1.7.6
django-auth-ldap==1.2.5
django-mssql==1.6.2
pyodbc==3.0.7
python-ldap==2.4.19
pywin32==219
virtualenv==12.0.7
I am using Django development server.
Does the error "Could not open a connection to SQL Server [2]" is more to the user authentication error or couldn't find the SQL server?
In this case, your HOST string needs to contain both computer name or address and an Instance ID Las set during the installation procedure (SQLEXPRESS by default).
Use:
'HOST': 'COMPUTER-NAME\\SQLEXPRESS'
or
'HOST': '127.0.0.1\\SQLEXPRESS'

Setup and connect to database for Django on AWS

I've currently hosted a Django application on EC2 using Apache.
My database engine
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'subscribe',
'USER': '<username>',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '3306',
}
}
After setting up Apache, i can now access my web application on the public IP but I cannot perform any DB transcations as the tables don't exist. This is the error message:
ProgrammingError at /some-url
(1146, "Table 'subscribe.subscriberapp_subscriber' doesn't exist")
I know for sure this is because no migrations have been made post deploying to AWS. My question is, how do I setup the DB completely?
You need to run the initial migrations to create the tables. From the console verify that you have a connection to your db by running ./manage.py dbshell. If that works, you have the connection.
Then you need to either run the initial ./manage.py syncdb (for django <1.7) or if you're running django 1.7+ you will run ./manage.py migrate

Categories