I trying to add test cases for the django application but the test pass successfully on the locally but it fails on the Travis CI.
M .travis.yml files looks like this:
dist: bionic
services:
- postgresql
addons:
postgresql: '9.5'
apt:
packages:
- postgresql-9.5
before_script:
- psql -c 'create database fecundity_test;' -U postgres
branches:
only:
- "master"
language: python
python:
- "3.8"
install:
- if [ "$TRAVIS_BRANCH" = "master" ]; then pip install -r requirements/dev.txt; fi
- if [ "$TRAVIS_BRANCH" = "master" ]; then pip install coveralls; fi
script:
- if [ "$TRAVIS_BRANCH" = "master" ]; then ./scripts/lib/run-ci; fi
after_success:
- if [ "$TRAVIS_BRANCH" = "master" ]; then coveralls; fi
And my settings.py looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['POSTGRES_DB'],
'USER': os.environ['POSTGRES_USER'],
'PASSWORD': os.environ['POSTGRES_PASSWORD'],
'HOST': os.environ['POSTGRES_HOST'],
'PORT': '',
'ATOMIC_REQUESTS': True,
'TEST': {
'NAME': os.environ['POSTGRES_TEST_DB']
}
},
}
When I run it on travis is get this error log:
$ if [ "$TRAVIS_BRANCH" = "master" ]; then ./scripts/lib/run-ci; fi
Using existing test database for alias 'default' ('fecundity_test')...
/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/backends/postgresql
/base.py:294: RuntimeWarning: Normally Django will use a connection to the 'postgres' database
to avoid running initialization queries against the production database when it's not needed
(for example, when running tests). Django was unable to create a connection to the 'postgres'
database and will use the first PostgreSQL database instead.
warnings.warn(
Traceback (most recent call last):
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
self.connect()
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/backends/base/base.py", line 197, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
connection = Database.connect(**conn_params)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "db" to address: Temporary failure in name resolution
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./manage.py", line 21, in <module>
main()
File "./manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
super().run_from_argv(argv)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/core/management/commands/test.py", line 53, in handle
failures = test_runner.run_tests(test_labels)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/test/runner.py", line 684, in run_tests
old_config = self.setup_databases(aliases=databases)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/test/runner.py", line 604, in setup_databases
return _setup_databases(
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/test/utils.py", line 169, in setup_databases
connection.creation.create_test_db(
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/backends/base/creation.py", line 58, in create_test_db
self._create_test_db(verbosity, autoclobber, keepdb)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/backends/base/creation.py", line 168, in _create_test_db
with self._nodb_connection.cursor() as cursor:
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/backends/base/base.py", line 260, in cursor
return self._cursor()
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/backends/base/base.py", line 236, in _cursor
self.ensure_connection()
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
self.connect()
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
self.connect()
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/backends/base/base.py", line 197, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
connection = Database.connect(**conn_params)
File "/home/travis/virtualenv/python3.8.1/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not translate host name "db" to address: Temporary failure in name resolution
The command "if [ "$TRAVIS_BRANCH" = "master" ]; then ./scripts/lib/run-ci; fi" exited with 1.
I have no idea what is working, And I tried to search on the internet but I could not find any help. And further CI is passing without tests. Thanks in Advance.
After so much effort I figured out that the reason to fail was that, I did not add EVN variables in Travis CI. If You are getting the same error make you have added the variables in the ENV settings of TravisCI.
Related
I am trying to create an app on django and I am trying to host it on heroku. Everything runs perfectly locally. I get an error while trying to migrate the database onto the heroku app by running command -
heroku run python manage.py migrate
This is the error I am getting -
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 211, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
connection = Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/__init__.py", line 121, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/extensions.py", line 167, in make_dsn
parse_dsn(dsn)
psycopg2.ProgrammingError: invalid dsn: invalid connection option "init_command"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/manage.py", line 22, in <module>
main()
File "/app/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 373, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 417, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 90, in wrapped
res = handle_func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 75, in handle
self.check(databases=[database])
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 438, in check
all_issues = checks.run_checks(
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/registry.py", line 77, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/model_checks.py", line 34, in check_all_models
errors.extend(model.check(**kwargs))
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 1307, in check
*cls._check_indexes(databases),
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 1699, in _check_indexes
connection.features.supports_covering_indexes or
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/features.py", line 84, in is_postgresql_11
return self.connection.pg_version >= 110000
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 339, in pg_version
with self.temporary_connection():
File "/app/.heroku/python/lib/python3.9/contextlib.py", line 119, in __enter__
return next(self.gen)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 614, in temporary_connection
with self.cursor() as cursor:
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 270, in cursor
return self._cursor()
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 246, in _cursor
self.ensure_connection()
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 211, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
connection = Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/__init__.py", line 121, in connect
dsn = _ext.make_dsn(dsn, **kwargs)
File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/extensions.py", line 167, in make_dsn
parse_dsn(dsn)
django.db.utils.ProgrammingError: invalid dsn: invalid connection option "init_command"
settings.py -
prod_db = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(prod_db)
settings.py ( database section ) -
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '****',
'USER': '****',
'PASSWORD': '****',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}
How can I solve this issue? Please ask if I need to add any of my files onto the question.
You are using MySQL for local development and PostgreSQL in production. The PostgreSQL connection is coming from the DATABASE_URL environment variable that dj_database_url uses by default.
Since you are updating your default database from the environment (not replacing it), the MySQL-specific init_command option is being left in your PostgreSQL configuration. The psycopg2 PostgreSQL driver doesn't know what to do with it.
A quick and dirty solution would be to completely your default database configuration instead of updating it:
DATABASES['default'] = prod_db
But I strongly urge you to use the same database server for development and production. As you have discovered, database engines aren't drop-in replacements for each other. In this case, it's likely easiest to switch from PostgreSQL to MySQL on Heroku.
First, let's get rid of the Postgres stuff:
Remove psycopg2 from your dependencies. Its presence likely caused Heroku to automatically provision a PostgreSQL database for you, which added to the confusion.
Optionally, de-provision the existing Postgres database. It's probably the free version so it isn't costing you anything, but it's a bit weird to have it hanging around if you are using a completely different database server.
Now, provision a MySQL server. I suggest you pick from the available addons. There are a few MySQL options, including JawsDB and ClearDB. Here is an example using ClearDB:
heroku addons:create cleardb:ignite
Ignite is the free tier for ClearDB. Once provisioned, your app will see a new environment variable CLEARDB_DATABASE_URL. Other addons will use other environment variable names, so make sure to read their documentation.
Finally, update your Django configuration. dj_database_url uses the DATABASE_URL environment variable by default, but we can tell it to use another:
prod_db = dj_database_url.config(env="CLEARDB_DATABASE_URL", conn_max_age=500)
DATABASES['default'].update(prod_db)
I start learning Django, and I had a problem using the Postegres database,
The server worked fine with sqlite3, but as soon as I declared Postgres in the project, the server stopped working
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql', # on utilise l'adaptateur postgresql
'NAME': 'sarah disquaires', # le nom de notre base de donnees creee precedemment
'USER': 'sarra', # attention : remplacez par votre nom d'utilisateur
'PASSWORD': '',
'HOST': '',
'PORT': '5432',
}
}
I get this error:
python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
self.connect()
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psycopg2\__init__.py", line 122, 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 "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
self.check_migrations()
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 486, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
self.build_graph()
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\loader.py", line 220, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations
if self.has_table():
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\migrations\recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 259, in cursor
return self._cursor()
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 235, in _cursor
self.ensure_connection()
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
self.connect()
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
self.connect()
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\sarra\AppData\Local\Programs\Python\Python38-32\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: fe_sendauth: no password supplied
I tried to migrate the base with python manage.py makemigrations and python manage.py migrate but didn't work,
I tested several solutions proposed on several sites, but did not work for me.
please help me solve this problem
As the error message says:
no password supplied
So add the database password to the configurarion dictionary and re-run the migration
I had a problem with my vagrant boxes with ports and everything so I did something that I suspect is inhibiting my ability to run ./manage.py migrate. Here's what it looks like when I run vagrant ssh-config
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/brock1hj/projects/sodium/to-vagrant/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
Here is the full error:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 93, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/db/migrations/executor.py", line 19, in __init__
self.loader = MigrationLoader(self.connection)
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/db/migrations/loader.py", line 47, in __init__
self.build_graph()
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/db/migrations/loader.py", line 180, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
self.ensure_schema()
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/db/backends/base/base.py", line 162, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/db/backends/base/base.py", line 135, in _cursor
self.ensure_connection()
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/db/backends/base/base.py", line 119, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection
connection = Database.connect(**conn_params)
File "/Users/brock1hj/envs/sodium/lib/python2.7/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL: password authentication failed for user "vagrant"
I'm really not sure what's going on and I think somehow the password for the user "vagrant" somehow got changed from the default "vagrant".
I am using Django 1.8.2
Here is my settings.py file for databases:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'vagrant',
'USER': 'vagrant',
'PASSWORD': 'vagrant',
'HOST': '127.0.0.1',
'PORT': '',
}
}
I feel like it is going to try putting "vagrant" as a password but that is no longer the case.
Trying to enter the dbshell, was prompted to enter the password to ensure "vagrant" didn't work and got:
Password for user vagrant:
psql: error: could not connect to server: could not initiate GSSAPI security context: Unspecified GSS failure.
Minor code may provide more information could not initiate GSSAPI security context: Configuration file does
not specify default realm
FATAL: password authentication failed for user "vagrant"
Try:
vagrant ssh
psql
Then follow instructions on https://www.postgresql.org/docs/9.6/sql-alterrole.html, looks like you want ALTER ROLE vagrant PASSWORD 'vagrant';
I am new to django, I was running the command
python manage.py migrate
and got this error.
(py1) G:\django\djangoproject1>python manage.py migrate Traceback
(most recent call last): File "manage.py", line 22, in
execute_from_command_line(sys.argv) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\management__init__.py",
line 364, in execute_from_command_line
utility.execute() File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\management__init__.py",
line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\management\base.py",
line 283, in run_from_argv
self.execute(*args, **cmd_options) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\management\base.py",
line 327, in execute
self.check() File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\management\base.py",
line 359, in check
include_deployment_checks=include_deployment_checks, File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\management\commands\migrate.py",
line 61, in _run_checks
issues = run_checks(tags=[Tags.database]) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\checks\registry.py",
line 81, in run_checks
new_errors = check(app_configs=app_configs) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\core\checks\database.py",
line 10, in check_database_backends
issues.extend(conn.validation.check(**kwargs)) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\mysql\validation.py",
line 9, in check
issues.extend(self._check_sql_mode(**kwargs)) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\mysql\validation.py",
line 13, in _check_sql_mode
with self.connection.cursor() as cursor: File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\base\base.py",
line 254, in cursor
return self._cursor() File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\base\base.py",
line 229, in _cursor
self.ensure_connection() File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\base\base.py",
line 213, in ensure_connection
self.connect() File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\utils.py", line
94, in exit
six.reraise(dj_exc_type, dj_exc_value, traceback) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\base\base.py",
line 213, in ensure_connection
self.connect() File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\base\base.py",
line 189, in connect
self.connection = self.get_new_connection(conn_params) File "C:\Users\Mahin\Envs\py1\lib\site-packages\django\db\backends\mysql\base.py",
line 274, in get_new_connection
conn = Database.connect(**conn_params) File "C:\Users\Mahin\Envs\py1\lib\site-packages\MySQLdb__init__.py", line
84, in Connect
return Connection(*args, **kwargs) File "C:\Users\Mahin\Envs\py1\lib\site-packages\MySQLdb\connections.py",
line 164, in init
super(Connection, self).init(*args, **kwargs2) django.db.utils.OperationalError: (1045, "Access denied for user
'root'#'localhost' (using password: YES)")
Since I should have magically known your settings.py DB config and my post was deleted for not answering the question without "requiring clarification from the asker" I'm reposting to try and help. The default password for root user in a mysql db is "", that is its left blank. Right now there is a mismatch between your db and the configuration you provided in your settings.py file. Go into your dbms and make sure that the setting for the database match the ones you provided in your settings.py.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB NAME',
'USER': 'USER NAME',
'PASSWORD':'USER PW',
'HOST' : 'localhost',
}
}
If these match to your db you should be able to now migrate.
SOLVED: I was using USERNAME, not USER from previously attempting Postgres driver.
I'm trying to connect my django project to an RDS MySQL database. I can connect fine using my credentials in MySQL Workbench and mysql command line.
I've set the AWS Security Group and VPC Security Group with
All traffic | All | All | 0.0.0.0/0
Let's say my credentials are
User: abc
Password: password
When I run python manage.py migrate it is attempting to login with Mike, not abc for some unknown reason (Mike is the Windows User). How do I prevent this? Attached is my settings.py:
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'playercomp',
'USERNAME': 'abc',
'PASSWORD': 'password',
'HOST': 'xxx.xxx.us-west-2.rds.amazonaws.com',
'PORT': '3306',
}
}
manage.py
#!/usr/bin/env python
try:
import pymysql
pymysql.install_as_MySQLdb()
except ImportError:
print "Failed to import pymysql"
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "playercomp.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
Error Trace:
Traceback (most recent call last):
File "manage.py", line 27, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
utility.execute()
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\management\__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\management\base.py", line 342, in execute
self.check()
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\management\base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\management\commands\migrate.py", line 61, in _run_checks
issues = run_checks(tags=[Tags.database])
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\core\checks\database.py", line 10, in check_database_backends
issues.extend(conn.validation.check(**kwargs))
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\mysql\validation.py", line 9, in check
issues.extend(self._check_sql_mode(**kwargs))
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\mysql\validation.py", line 13, in _check_sql_mode
with self.connection.cursor() as cursor:
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\base\base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\base\base.py", line 204, in _cursor
self.ensure_connection()
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\base\base.py", line 199, in ensure_connection
self.connect()
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\base\base.py", line 199, in ensure_connection
self.connect()
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\base\base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\django\db\backends\mysql\base.py", line 263, in get_new_connection
conn = Database.connect(**conn_params)
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\pymysql\connections.py", line 688, in __init__
self.connect()
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\pymysql\connections.py", line 906, in connect
self._request_authentication()
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\pymysql\connections.py", line 1114, in _request_authentication
auth_packet = self._read_packet()
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\pymysql\connections.py", line 981, in _read_packet
packet.check_error()
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\pymysql\connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "C:\Users\Mike\Envs\playercomp\lib\site-packages\pymysql\err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
django.db.utils.OperationalError: (1045, u"Access denied for user 'Mike'#'host-yy-yy.zzz.zz.com' (using password: YES)")
I added a field 'USER': 'abc' to the DATABASES default, and that worked.