How to set up a PostgreSQL database in Django

后端 未结 11 1124
滥情空心
滥情空心 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:45

    I was having the same Issue on Mac.

    The solution was to use only PIP to install everything, and touch some things.

    First install PIP from: https://pip.pypa.io/en/latest/

    Then you want to make sure if path to pg_config is in your PATH (echo $PATH), if not you can edit your bash_profile:

    vi /Users/<user>/.bash_profile
    

    and add this line:

    export PATH=$PATH:/path/to/pg_config/bin
    

    If you don't know where pg_config is you can use the "locate" tool, but be sure your locate.db is up to date (i was using an old locate.db and using paths that does not exists).

    sudo /usr/libexec/locate.updatedb
    locate pg_config
    

    Then install Django (if needed) and psycopg2.

    sudo pip install Django
    sudo pip install psycopg2
    

    And then in settings.py (localhost:defaultport)

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

    Greets!

    0 讨论(0)
  • 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': '',
        }
    }
    
    0 讨论(0)
  • 2020-11-29 15:55

    Also make sure you have the PostgreSQL development package installed. On Ubuntu you need to do something like this:

    $ sudo apt-get install libpq-dev
    
    0 讨论(0)
  • 2020-11-29 15:55

    The immediate problem seems to be that you're missing the psycopg module.

    0 讨论(0)
  • 2020-11-29 15:55

    This is one of the very good and step by step process to set up PostgreSQL in ubuntu server. I have tried it with Ubuntu 16.04 and its working.

    https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-14-04

    0 讨论(0)
提交回复
热议问题