Skip to content

Instantly share code, notes, and snippets.

@ehbc221
Created December 1, 2018 21:46
Show Gist options
  • Save ehbc221/7ea5bf2d359a0889da5ac5a87184b2cd to your computer and use it in GitHub Desktop.
Save ehbc221/7ea5bf2d359a0889da5ac5a87184b2cd to your computer and use it in GitHub Desktop.
Configuration to use for Postgres SQL in Django projects
1) Our first step will be install all of the pieces that we need from the repositories.
We will install pip, the Python package manager, in order to install and manage our Python components.
We will also install the database software and the associated libraries required to interact with them.
sudo apt-get update
sudo apt-get install python3-pip python3-dev libpq-dev postgresql postgresql-contrib
2) Log into an interactive Postgres session by typing:
sudo -u postgres psql
3) First, we will create a database for our Django project.
Each project should have its own isolated database for security reasons.
We will call our database myproject in this guide, but it's always better to select something more descriptive:
CREATE DATABASE myproject;
4) Next, we will create a database user which we will use to connect to and interact with the database.
Set the password to something strong and secure:
CREATE USER myprojectuser WITH PASSWORD 'password';
5) Setting database parameters.
This will speed up database operations so that the correct values do not have to be queried and set each time a connection is established.
ALTER ROLE myprojectuser SET client_encoding TO 'utf8';
ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myprojectuser SET timezone TO 'UTC';
6) Now, all we need to do is give our database user access rights to the database we created:
GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;
7) Exit the SQL prompt to get back to the postgres user's shell session:
\q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment