简体   繁体   中英

python migrate (django) not working with postgresql

I installed psycopg2 and followed all the steps given in this tutorial as well as django docs when installing postgresql: http://killtheyak.com/use-postgresql-with-django-flask/

But I'm getting this exception when running python manage.py migrate

Traceback (most recent call last)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
connection = Database.connect(**conn_params)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: FATAL:  role "user" does not exist


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
connection = Database.connect(**conn_params)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: FATAL:  role "user" does not exist


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 89, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/migrations/executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/migrations/loader.py", line 176, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
self.ensure_schema()
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/migrations/recorder.py", line 52, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/backends/base/base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/backends/base/base.py", line 204, in _cursor
self.ensure_connection()
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
connection = Database.connect(**conn_params)
File "/Users/mehulgoel/Documents/workspace/python/django18/lib/python3.4/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: FATAL:  role "user" does not exist

Any help would be appreciated.

I suppose the user you created here:

sudo -u postgres createuser -s $USER

is not like the user you use here:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'yournewdb',
        'USER': 'yourusername',
        'PASSWORD': 'yourpassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

If you're using Linux, install dependencies for PostgreSQL to work with Python:

sudo apt install libpq-dev python-dev

Then install PostgreSQL with the following command:

sudo apt install postgresql postgresql-contrib

If you are on mac or windows download PostgreSQL and install it.

Also, you'd have to use psycopg2 as the python adapter for PostgreSQL.

pip install psycopg2-binary

Once it is all done, use the following commands:

sudo -u postgres psql
postgres=# create database mydb;
postgres=# create user myuser with encrypted password 'mypass';
postgres=# grant all privileges on database mydb to myuser;

After doing all this modify your settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypass',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

and then,

python manage.py migrate

It should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM