225π
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.
31π
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
- [Django]-Django: remove a filter condition from a queryset
- [Django]-Django templates: verbose version of a choice
- [Django]-Extend base.html problem
27π
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 pgadmin4
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': '',
}
}
- [Django]-Extend base.html problem
- [Django]-Django rest framework lookup_field through OneToOneField
- [Django]-Disable migrations when running unit tests in Django 1.7
7π
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.
- [Django]-Django render_to_string missing information
- [Django]-Django REST Framework: adding additional field to ModelSerializer
- [Django]-How to add multiple objects to ManyToMany relationship at once in Django ?
6π
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.
- [Django]-Many-To-Many Fields View on Django Admin
- [Django]-Delete multiple objects in django
- [Django]-Django REST Framework custom fields validation
- [Django]-Django ModelForm: What is save(commit=False) used for?
- [Django]-">", "<", ">=" and "<=" don't work with "filter()" in Django
- [Django]-How to access a dictionary element in a Django template?
4π
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.
- [Django]-Django: How can I create a multiple select form?
- [Django]-Django β how to unit test a post request using request.FILES
- [Django]-How can I upgrade specific packages using pip and a requirements file?
3π
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!
- [Django]-How to view corresponding SQL query of the Django ORM's queryset?
- [Django]-How to produce a 303 Http Response in Django?
- [Django]-PyCharm: DJANGO_SETTINGS_MODULE is undefined
2π
$ sudo apt-get install libpq-dev
Year, this solve my problem.
After execute this, do:
pip install psycopg2
- [Django]-How to reload modules in django shell?
- [Django]-Set up a scheduled job?
- [Django]-How exactly do Django content types work?
1π
Steps to create a Django app with PostgreSQL database from scratch.
-
Check if PostgreSQL is installed in your system. In your bash shell enter
psql --version
-
If PostgreSQL is installed jump to step 6.
-
To install PostgreSQL β
sudo apt-get install python3-dev libpq-dev sudo apt-get install postgresql postgresql-contrib
-
By default PostgreSQL installation creates a special user named "postgres" that has all rights. To login to PostgreSQL
sudo -u postgres psql
To exit PostgreSQL session
\q
-
Now set password for "postgres" user.
Login to PostgreSQL β
sudo -u postgres psql
In PostgreSQL interactive session enter β
\password postgres
-
Create PostgreSQL database.
To create directly from bash shell β
sudo -u postgres createdb your_db_name
To create from PostgreSQL interactive session β
createdb your_db_name
To check if the database is created, list all databases using PostgreSQL command
\l
-
If you already have an existing Django project then jump to step 11. Otherwise create a virtual environment. I use virtualenv wrapper.
mkvirtualenv your_project_name
-
Create Django project.
django-admin startproject your_project_name
-
Cd to the project directory and create your app.
python manage.py startapp your_app_name
-
Install Django.
pip install django
-
Install Psycopg2. Psycopg2 is a PostgreSQL database adapter for Python. This is required for Django to connect with PostgreSQL.
pip install psycopg2
-
Update settings.py as follows
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'your_db_name', 'USER': 'postgres', 'PASSWORD': 'xxxxx', 'HOST': 'localhost', 'PORT': '', } }
-
Run migration.
python3 manage.py migrate
-
Run Django server and visit localhost:8000
python3 manage.py runserver 0.0.0.0:8000
-
If required you can install pgadmin β a graphical client to view and manipulate PostgreSQL database schema and data.
- [Django]-Error when using django.template
- [Django]-Setting Django up to use MySQL
- [Django]-Can I access constants in settings.py from templates in Django?
0π
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.
- [Django]-CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true
- [Django]-What is the SQL ''LIKE" equivalent on Django ORM queries?
- [Django]-How can I see the raw SQL queries Django is running?
0π
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.
- [Django]-How do I do an OR filter in a Django query?
- [Django]-Backwards migration with Django South
- [Django]-Extend base.html problem
- [Django]-How do I POST with jQuery/Ajax in Django?
- [Django]-How can I create a deep clone of a DB object in Django?
- [Django]-Django.db.utils.ProgrammingError: relation already exists