How to set up a PostgreSQL database in Django

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

    Please note that installation of psycopg2 via pip or setup.py requires to have Visual Studio 2008 (more precisely executable file vcvarsall.bat). If you don't have admin rights to install it or set the appropriate PATH variable on Windows, you can download already compiled library from here.

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

    If you are using Fedora 20, Django 1.6.5, postgresql 9.3.* and you need the psycopg2 module, do this:

    yum install postgresql-devel
    easy_install psycopg2
    

    If you are like me, you may have trouble finding the well documented libpq-dev rpm... The above worked for me just now.

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

    This may seem a bit lengthy, but it worked for me without any error.

    At first, Install phppgadmin from Ubuntu Software Center.

    Then run these steps in terminal.

    sudo apt-get install libpq-dev python-dev
    pip install psycopg2
    sudo apt-get install postgresql postgresql-contrib phppgadmin
    

    Start the apache server

    sudo service apache2 start
    

    Now run this too in terminal, to edit the apache file.

    sudo gedit /etc/apache2/apache2.conf
    

    Add the following line to the opened file:

    Include /etc/apache2/conf.d/phppgadmin
    

    Now reload apache. Use terminal.

    sudo /etc/init.d/apache2 reload
    

    Now you will have to create a new database. Login as 'postgres' user. Continue in terminal.

    sudo su - postgres
    

    In case you have trouble with the password of 'postgres', you can change it using the answer here https://stackoverflow.com/a/12721020/1990793 and continue with the steps.

    Now create a database

    createdb <db_name>
    

    Now create a new user to login to phppgadmin later, providing a new password.

    createuser -P <new_user>
    

    Now your postgressql has been setup, and you can go to:

    http://localhost/phppgadmin/
    

    and login using the new user you've created, in order to view the database.

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

    You can install "psycopg" with the following command:

    # sudo easy_install psycopg2
    

    Alternatively, you can use pip :

    # pip install psycopg2
    

    easy_install and pip are included with ActivePython, or manually installed from the respective project sites.

    Or, simply get the pre-built Windows installer.

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

    Year, this solve my problem. After execute this, do: pip install psycopg2

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

    You need to install psycopg2 Python library.

    Installation


    Download http://initd.org/psycopg/, then install it under Python PATH

    After downloading, easily extract the tarball and:

    $ python setup.py install
    

    Or if you wish, install it by either easy_install or pip.

    (I prefer to use pip over easy_install for no reason.)

    • $ easy_install psycopg2
    • $ pip install psycopg2

    Configuration


    in settings.py

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'db_name',                      
            'USER': 'db_user',
            'PASSWORD': 'db_user_password',
            'HOST': '',
            'PORT': 'db_port_number',
        }
    }
    

    - Other installation instructions can be found at download page and install page.

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