v18o
v18o

Reputation: 1297

Permission denied error with PostgreSQL and Django. Giving rights to PostgreSQL user admin

I have Django app with PostgreSQL.

The app has those environment variables:

DATABASE_HOST=localhost
DATABASE_USER=admin
DATABASE_PASSWORD=admin

Here is psql output:

postgres=# CREATE USER admin WITH PASSWORD 'admin123';
ERROR:  role "admin" already exists


postgres=# select * from USER;
 current_user
--------------
 postgres
(1 row)

postgres=# GRANT ALL privileges ON DATABASE my_db to admin;                 
GRANT

When I try to take something from db I get ProgrammingError: permission denied for relation app_rangeslot.

So, the questions:

1) If user admin have all rights, why I get permission denied error?

2) If user admin is created, why I cannot see it?

Upvotes: 1

Views: 924

Answers (2)

Neil Anderson
Neil Anderson

Reputation: 1365

I think you are connected as the postgres user

      'USER': os.getenv('DATABASE_USER', 'postgres'),

don't you want to actually connect as the admin?

      'USER': os.getenv('DATABASE_USER', 'admin'),

Upvotes: 0

Mohammad Mustaqeem
Mohammad Mustaqeem

Reputation: 1084

I think you are missing 'default' key in the DATABASES dict. Your settings should be as given below:

DATABASES = {
  'default': {
      'ENGINE': 'django.db.backends.postgresql_psycopg2',
      'NAME': os.getenv('DATABASE_NAME', 'my_dev'),
      'USER': os.getenv('DATABASE_USER', 'postgres'),
      'PASSWORD': os.getenv('DATABASE_PASSWORD', ''),
      'HOST': os.getenv('DATABASE_HOST', ''),
      'PORT': '5432',
  }
}

Upvotes: 1

Related Questions