2
votes

I am new to Django and I am trying to create table in Postgre using Django(1.8)

following is my model class

    class Student(models.Model):
        name = models.CharField(max_length = 50)
        degree = models.CharField(max_length = 50)
        numofsubs = models.IntegerField()
        namesofsubs= models.CharField(max_length = 50)
        details = models.CharField(max_length = 50)

class Meta:

      db_table = "student"

views.py

def addStudent(request):
    student = Student(name = request.name, degree = request.degree ,
     numofsubs = request.numofsubs , nameofsubs = request.nameofparams , details = request.details)
    student.save()
    print 'data saved'

After these changes when I tried to run python manage.py migrate i got django.db.utils.ProgrammingError: permission denied for relation django_migrations

following are the stack traces

Traceback (most recent call last): File "manage.py", line 10, in execute_from_command_line(sys.argv) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/core/management/init.py", line 338, in execute_from_command_line utility.execute() File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/core/management/init.py", line 330, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv self.execute(*args, **cmd_options) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute output = self.handle(*args, **options) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 93, in handle executor = MigrationExecutor(connection, self.migration_progress_callback) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 19, in init self.loader = MigrationLoader(self.connection) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 47, in init self.build_graph() File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 180, in build_graph self.applied_migrations = recorder.applied_migrations() File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 60, in applied_migrations return set(tuple(x) for x in self.migration_qs.values_list("app", "name")) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/models/query.py", line 162, in iter self._fetch_all() File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/models/query.py", line 965, in _fetch_all self._result_cache = list(self.iterator()) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/models/query.py", line 1220, in iterator for row in compiler.results_iter(): File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 783, in results_iter results = self.execute_sql(MULTI) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 829, in execute_sql cursor.execute(sql, params) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in exit six.reraise(dj_exc_type, dj_exc_value, traceback) File "/usr/lib/ckan/default/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: permission denied for relation django_migrations

My settings.py has following conf for db conection

DATABASES = {
    'default': {
       # 'ENGINE': 'django.db.backends.sqlite3',
       # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'abc',
        'USER': 'xyz',
        'PASSWORD': 'xxxxx',
        'HOST': 'localhost',
        'PORT': 5432,
    }
}

Please guide me whats wrong with my app.

Thanks

2
did you create postgres database with your django user as owner?Brown Bear
I created db while installing postgres via shel , and now i want to connect to that db through django and create and insert data in tableM.Ahsen Taqi
can you test local connection psql -h localhost -U xyz abc ?Brown Bear

2 Answers

1
votes

May be you need to allow grands to your user:

GRANT ALL ON DATABASE abc TO xyz;
1
votes

I would not suggest to do a GRANT ALL. However, as Bear Brown pointed out, it sounds like it's down to permissions, so make sure you at least have USAGE for relevant schema and the SELECT privilege for the table. First connect to the abc database, and then grant the select privilege on public:

GRANT SELECT ON ALL TABLES IN SCHEMA public TO xyz