24

I'm a db dummy, and im trying to set up PostgreSQL for my django project. For that I also use psycopg2. Databases are complicated though. Personally I would like if there was a way to get hold of ALL my DATABASE- and USER-SETTINGS/INFO in one place. So I knew what connect to and how (I'm still running local so there's no security-issue with that?).

However it seems I don't have the "rights" to create this database, even though I connect to the standard "admin"-user "postgres". With the password i typed in under installation ("pw123").

Django-project (settings.py):

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'django',                      
        'USER': 'postgres',
        'PASSWORD': 'pw123',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

CMD -> python manage.py shell (after importing django.db connection)

>>> cursor = connection.cursor()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 165, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 138, in _cursor
    self.ensure_connection()
  File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 133, in ensure_connection
    self.connect()
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 133, in ensure_connection
    self.connect()
  File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 122, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Python27\lib\site-packages\django\db\backends\postgresql_psycopg2\base.py", line 134, in get_new_connection
    return Database.connect(**conn_params)
  File "C:\Python27\lib\site-packages\psycopg2-2.5.4-py2.7-win32.egg\psycopg2\__init__.py", line 164, in connect
    conn = _connect(dsn, connection_factory=connection_factory, async=async)
OperationalError: FATAL:  database "django" does not exist

Have I done something wrong in the installation? Can I correct it? Should I consider reinstalling everything? How would I go about that? Databases are confusing me :P

howtopythonpls
  • 770
  • 1
  • 6
  • 18

4 Answers4

35

PostgreSQL does not auto-create databases on first connection. You must create a database before you can use it.

Connect to PostgreSQL (usually to the administration database, named 'postgres'), via PgAdmin-III or the command-line psql client, and create the database django. From PgAdmin-III you can do this via the menus; from psql you use the CREATE DATABASE SQL command.

See also Creating a PostgreSQL database in the manual and this tutorial I found in a 5second google search that doesn't look totally wrong.

What I'd do is connect using psql (you can find it in the Start menu) as the 'postgres' user, with the password you set at install time, then:

CREATE USER django WITH PASSWORD 'somepassword';

CREATE DATABASE django WITH OWNER django ENCODING 'utf-8';
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • I didn't know psql was a command-prompt, now I understand were all the commands go. Sorry I have Windows 8. I will try this out! Thank you! – howtopythonpls Oct 15 '14 at 10:17
  • @howtopythonpls It's not on the default PATH so unless you changed your PATH you'll want something like `%PROGRAMFILES%\PostgreSQL\9.3\bin\psql` if stating it directly from the command prompt. Or you can just use the Start menu entry for it. – Craig Ringer Oct 15 '14 at 10:18
  • Users shouldn't have to manually create a database.. Wtf? Ruby on Rails automates this with `rails db:create` and it works on ALL database types. – Ryan Glenn Feb 27 '22 at 20:07
6

It's because unlike SQLite, you need to create the database, you can follow this guide https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn#step-seven-configure-postgresql

fasouto
  • 4,386
  • 3
  • 30
  • 66
  • I found similair guides, but what is "sudo"? I am running on Windows, is this possible still? – howtopythonpls Oct 14 '14 at 22:00
  • @howtopythonpls The use of `sudo` is unix/linux specific, and refers to the default authentication mode PostgreSQL uses on Linux. On Windows it defaults to password authentication, so you do not need and cannot use 'sudo' or its Windows equivalent, 'runas'. – Craig Ringer Oct 15 '14 at 03:26
2

Here 'NAME' means user name which is by default 'postgres' . So 'NAME' should be username not database name . Try this it's works

Gopal
  • 21
  • 1
1

All you need to do is to create the database with name django in your postgresql. By default the user postgres will have privilege on database django and password will the password for user postgres.