Brew Postgresql
Introduction
Django is a fast, high level developmental framework that allows for developing a web application in Python with a lot less hassle than some other frameworks. While Django automatically stores data in a SQLite database, this Django tutorial for postgresql will explain how to store data in Python with the Django framework using the PostgreSQL database cluster.
Thanks so much for putting this together! Drivers slacker usb devices. It looks like the brew command has been updated, though. It's now brew install postgres and not brew install postgresql. Thanks for the comment, I've updated to postgres instead of postgresql since it's the latest / greatest naming:).
Prerequisites
It must be confirmed whether the PostgreSQL database is installed on the local machine, as it will require the credentials that will be set to the Django application. Execute the
sudo systemctl status postgresql
command in a Systemd distro of Linux terminal to confirm the system is running, or just use thepg_ctl status
command.For MacOS, start the Homebrew PostgreSQL service with the
brew services start postgresql
command and then execute thebrew info postgres
command.Python must be installed and working properly. Python 3 is recommended as Python 2.7 is being deprecated and losing support.
Enter the psql console
Execute the following command to enter the interactive PostgreSQL terminal that corresponds to the access of the PostgreSQL administrative user:
- Homebrew is a package manager for Mac OS X that builds software from its source code. It includes a version of PostgreSQL packaged by what it refers to as a formula. This type of installation might be preferred by people who are comfortable using the command line to install programs, such as software developers. Typical use looks like.
- To start PostgreSQL run the following command in the Terminal. $ brew services start postgres We will get a similar output shown below. Successfully started `postgresql` (label: homebrew.mxcl.postgresql).
- Upgrading PostgreSQL 9.2 to 9.3 with Homebrew. What to do when PostgreSQL fails to start after a brew upgrade. Published September 23, 2013 Revised December 27, 2014: These instructions have also been tested for 9.3→9.4!
sudosu - postgres |
Now enter the sudo password or the root authentication for the device and press the -kbd-ENTER-/kbd- key.
When inside the PostgreSQL shell session, execute the following command to enter the console:
Create database and user
After entering the psql command-line console, a database and user must be created that can be set for the Django application. Execute the following command to create the database:
CREATEDATABASE djangodb; |
Now execute the following command to create a user login and password that will grant privileges to the newly created database:
Next, modify the parameters for the connection to the Django framework. This is needed to set up the operations for the value queries when a connection is established.
Execute the following ALTER ROLE
SQL statement to modify the parameters:
ALTERROLE orkb SET client_encoding TO'utf-8'; |
NOTE: This operation will set the encoding to UTF-8, the default expected in Django framework.
Execute the following command to the default transaction isolation:
ALTERROLE orkb SET default_transaction_isolation TO'read committed'; |
Brew Postgresql Client
NOTE: This will set the default transaction isolation each time access is granted to make a query that avoids the transactions that are not committed.
Execute the following command to set the timezone:
NOTE: Django typically uses the UTC, by default, for the timezone.
Finally, execute the following command to grant the user access privileges to the database:
GRANTALL PRIVILEGES ONDATABASE djangodb TO orkb; |
The results should resemble the following image:
Install Django in Virtual Environment
A virtual environment is used to manage the dependencies of different projects by creating a separation of Python isolation to the virtual environments.
Using the terminal, execute the following command to create a directory in the Django framework to store the project:
cd ~/orkb |
Now execute the following command to create a virtual environment that will store the Django project in Python.
NOTE: The venv
command is now a built-in feature of Python since version 3.6. If using a Python version older than 3.6, the virtual environment package for Python must be installed and activated before installing the Django framework.
Execute the following command to activate the virtual environment:
source venv/bin/activate |
With the venv activated, install the Django framework using the following pip3 command:
Now execute the following command to install the psycopg2 to allow configuring of PostgreSQL:
pip3 install psycopg2 |
Start the Django project
Using the below command will allow for creating a folder to hold the codes within the project directory. Note that the manage.py is created after putting a dot at the end of the command to set the project correctly.
Execute the following command to start the Django project:
With the project now created, execute the following command to configure the project to set the PostgreSQL database credentials:
nano venv/settings.py |
Look for the default configured database as SQLite and set it for the database credentials in PostgreSQL as follows:
DATABASES ={ 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'djangoproject', 'USER': 'orkb', 'PASSWORD': 'mypass123', 'HOST': 'localhost', 'PORT': ', } } |
Change the ALLOWED_HOSTS
Python list of allowable domains, as shown below, and add the domain’s IP address or localhost
for local development:
The results should resemble the following:
Test the Django application
With the configuration finished, the structures of data must be migrated to the database to test the application for the Django server.
Execute the following command to access the project directory:
cd ~/orkb |
Brew Postgresql-upgrade-database
Now add the following code to create a database structure:
python3 manage.py migrate |

Finally, execute the following code to create an administrative account for the Django application:
python3 manage.py createsuperuser Username (leave blank to use 'linux'): orkb Email address: orkb@email.com Password: Password (again): Superuser created successfully. |
NOTE: A current admin account can be used to set up a superuser account for the Django application.
Brew Postgresql Start
Now execute the following command to test the Django application and confirm it is working properly:
When the system displays a message that the application is working, add /admin
in the URL and enter the previously set up username and password.
Conclusion

Macos Restart Postgres
This Django tutorial for postgresql explained how to store data in Python with the Django framework using PostgreSQL. The tutorial explained how to create a database and user, grant the user access to the database, install Django in a virtual environment and activate the virtual environment. The Django tutorial for postgresql also explained how to start the Django project, create an administrative account and finally test the Django application. Remember that the virtual environment package for Python must be installed and activated separately, before installing the Django framework, if using a version of Python prior to 3.6.
