In our latest project we are using the Django Framework and we decided to use Postgres as the backend. Following are the steps
Change the DATABASES variable in the settings.py file.
DATABASES = {
default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'HOST': 'localhost',
'PORT': '5432',
}
}
The settings will make sense as we continue with the steps.
The next step would be to download the PostgresApp
To enable Django to connect to the Postgres DB we have to make sure that psycopg2 is installed. psycopg2 provides a Python DB API which a python framework like Django can use. The easiest way to install psycopg2 is using pip. Just do pip install psycopg2 and we are done. Or are we?
One of the most common error that is thrown while installing psycopg2 ( Mac ) is
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
This is easily fixable. All one has to do is
export PATH=/Applications/Postgres.app/Contents/Versions/<Version>/bin:$PATH
We are just adding the directory containing pg_config to the system path as the error instructed us to do. Running pip install psycopg2 should install it now. The next step is to get the postgres server started on our machine and create the tables required for the models in our app.
When we start the Postgres app a postgres server is started at port 5432. This is the reason why we have the HOST as localhost and port as 5432 in our settings file.
Next in the terminal for our postgres server we type the command
CREATE DATABASE postgres WITH OWNER postgres;
which creates a database named postgres with a superuser also named postgres (as mentioned in the settings file).
We are almost ready. The last step is to create migrations and migrate them
python manage.py makemigrations
python manage.py migrate
Our Postgres database is setup and ready to use.
Check out some apps that I built :
https://itunes.apple.com/au/app/vocab-watch/id1393813585
https://itunes.apple.com/app/handwrite-me/id1242134183
https://itunes.apple.com/app/lookie-dictionary-made-easy/id1219869272