Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operational Error: FATAL: database "django" does not exist

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

like image 208
howtopythonpls Avatar asked Oct 14 '14 20:10

howtopythonpls


3 Answers

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';
like image 152
Craig Ringer Avatar answered Oct 14 '22 18:10

Craig Ringer


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

like image 29
fasouto Avatar answered Oct 14 '22 19:10

fasouto


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

like image 2
Gopal Avatar answered Oct 14 '22 17:10

Gopal