How to set up a PostgreSQL database in Django

后端 未结 11 1164
滥情空心
滥情空心 2020-11-29 15:28

I\'m new to Python and Django.

I\'m configuring a Django project using a PostgreSQL database engine backend, But I\'m getting errors on each database operation. For

11条回答
  •  一生所求
    2020-11-29 15:46

    Step by step that I use:

     - sudo apt-get install python-dev
     - sudo apt-get install postgresql-server-dev-9.1
     - sudo apt-get install python-psycopg2 - Or sudo pip install psycopg2
    

    You may want to install a graphic tool to manage your databases, for that you can do:

    sudo apt-get install postgresql pgadmin3 
    

    After, you must change Postgre user password, then do:

     - sudo su
     - su postgres -c psql postgres
     - ALTER USER postgres WITH PASSWORD 'YourPassWordHere';
     - \q
    

    On your settings.py file you do:

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

    Extra:

    If you want to create the db using the command line you can just do:

    - sudo su
    - su postgres -c psql postgres
    - CREATE DATABASE dbname;
    - CREATE USER djangouser WITH ENCRYPTED PASSWORD 'myPasswordHere';
    - GRANT ALL PRIVILEGES ON DATABASE dbname TO djangouser;
    

    On your settings.py file you do:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'dbname',
            'USER': 'djangouser',
            'PASSWORD': 'myPasswordHere',
            'HOST': '',
            'PORT': '',
        }
    }
    

提交回复
热议问题