7

I'm taking EdX classes that use Ruby on Rails and python. That has given me courage to try and install and learn Django using Apach, mod_wsgi, and PostgreSQL. Following the detailed installation instructions, I first installed Apache, then mod_wsgi, and then PostgreSQL. I installed each from source and went through a little bit of tutorial with each and made sure they were properly installed. I've got a postgres user setup to run the PostgreSQL server and I was able to create a user "role" for myself as well as an admin role that my role inherits from that can create a database etc. I tried out some SQL in psql following a tutorial to make tables etc. I know how to grant privileges for a given role.

So anyway, I'm pretty close to the step where I would actually install Django from source, but I'm not sure how to follow this advice from the installation instructions:

If you plan to use Django's manage.py syncdb command to automatically create database tables for your models, you'll need to ensure that Django has permission to create and alter tables in the database you're using; if you plan to manually create the tables, you can simply grant Django SELECT, INSERT, UPDATE and DELETE permissions.

Maybe after I follow the steps to actually install Django and go through some tutorials, I'll understand exactly what needs to be setup in PostgreSQL to grant Django those permissions, but if I follow the installation instructions in order, it would seem to be saying I should setup those permissions now before installing Django. If I can get someone to tell me how to do it here before I do the install of Django, I'd appreciate it.

3 Answers 3

10

In the settings.py file of a django project, there is a snippet that says something like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'myproj_db',
        'USER': 'myproj_user',
        'PASSWORD': '123',
        'HOST': '',
        'PORT': '',
    },
}

What this does is that it tells Django what user (postgres user in this case) and database is used in conjunction with your django project.

Normally, you will need to create this myproj_user together with the myproj_db.

When you create this user, you can choose to give it permissions like so:

CREATE USER myproj_user WITH PASSWORD '123' SUPERUSER CREATEDB CREATEROLE LOGIN;

This creates the myproj_user with superuser, createdbm createrole, login permissions allowed to the user.

And then the database like so:

CREATE DATABASE myproj_db WITH OWNER myproj_user TEMPLATE=template1 ENCODING='utf-8';

3
  • 1
    Thanks. That's the direct answer to my question. Is it really necessary to specify further permissions after giving it SUPERUSER permissions? Also, should Django be a SUPERUSER? Nov 13, 2012 at 0:17
  • Not sure why some of my comments don't show up. Hope this doesn't show up twice. I wanted to thank you for also showing the correct way to set 'ENGINE' because when I set it according to comments in settings.py it was leading to an error about the module not being found. Nov 13, 2012 at 1:52
  • No problem! Glad you found it helpful. Certain database level operations require extra permissions. For example, the COPY FROM command requires the user to have SUPERUSER permissions. So it kinda depends on your requirements as to what permissions are needed. You will definitely need the LOGIN perm as this determines whether the user is able to authenticate and login against the db or not. CREATE USER actually assumes LOGIN so I didnt need to put it in my SQL statement above.
    – super9
    Nov 13, 2012 at 3:38
0

You say that you know how to grant privileges for a given role, which is what you need to do for Django before installing it (running syncdb).

This is part of setting up your database for use with Django – a step you take before creating each Django project. Each Django project corresponds to a site that you build with Django and is completely separate from another Django project. To each project belongs a database.* You can install Django the framework before you setup your database, because Django the framework doesn't do anything on its own.

Either you give Django permissions to create tables for you, in which case it can create tables for you (using manage.py syncdb). Or, you use manage.py sqlall <app> to get SQL that you run yourself to create the tables needed (which might be nice if you're paranoid about security).

To grant all permissions to a user for a specific database (option 1) in Postgres, use the command (from psql):

GRANT ALL PRIVILEGES ON DATABASE <db-name> TO <username>;

* Technically, they can share a database by simply configuring them to use the same one.

1
  • What I'm not understanding is what is the Django user? Do I just setup a role using psql for django to use, calling it whatever I want? Then, how do I tell Django what the name of that role is? Nov 12, 2012 at 3:35
0

Django uses ORM (Object Relational Mapper). What that means is that you do not directly deal with database tables for querying things however you deal with certain classes which then deal with the database. This is very useful and much more user-friendly then doing manually SQL. Consider the following example:

SELECT * FROM foo WHERE field="value"

vs

Foo.objects.filter(field='value')

In ORM, you describe the tables you have by making certain classes (in Django we call them models) and those models correspond to tables in the db, and their fields correspond to the columns in the db table. The following are very similar:

CREATE TABLE foo (
    title        varchar(50),
    description  text,
    likes        integer
);

and

class Foo(models.Model):
    title = models.CharField(max_length=50)
    description = models.TextField()
    likes = models.IntegerField()

However it is waste of time for you as a developer to construct the SQL statements for creating tables, and describing those tables in Python as models. Not to do that, Django allows you once you define all your models, to create db tables for you. That is the purpose of the syncdb command. It takes all the installed apps and models within them and creates tables within your database for them. However as you already mentioned in your question, databases have roles and those roles have permissions. For example, one of the permissions is CREATE as described here. So what Django documentation is saying is for you to grand all necessary permission to a role which Django will use to interact with the db for Django to be able to create necessary db tables it needs as well as possibly modify them later.

2
  • So, what I still need to understand is: Once I've created a role for Django to use, perhaps using SQL statements in a psql session for a particular database, how do I tell Django to use that role? Also, do I have to setup a different role for each database/project, or can there be just one Django role in PostgreSQL that Django always uses to interact with any database? Also, it sounds like it's really syncdb command that needs access. So, do I specify the role on the command line for syncdb? Nov 12, 2012 at 3:43
  • 1
    You specify the role in the Django's settings.py. More on that is in Django tutorial. As for the number of roles - its up to you whether to have a role for each project or to have a single role for all projects.
    – miki725
    Nov 12, 2012 at 3:48

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.