Create postgres database

  • after installing postgres, install psycopg2 library in your venv pipenv install psycopg2
  • if you installed postgres with homebrew, you can run just  createdb "DB_NAME" and it will create a db with default user and no password
  • you can also create a db manually in the db client (I use Postico)
  • or with psql (postgres terminal)

# create database and user, give him all privileges to that db
psql -U postgres

postgres=# create database DB_NAME;
postgres=# create user USER_NAME with encrypted password 'PASSWORD';
postgres=# grant all privileges on database DB_NAME to USER_NAME;


# create database with already created user
psql -U tystar

tystar=# create database DB_NAME;

  • in settings.py search for DATABASES and insert instead of sqlite
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

# example
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'testing_db',
        'USER': 'tystar',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}
  • run pipenv run python manage.py migrate (venv + python manage.py migrate)


Resources:

➡️https://medium.com/coding-blocks/creating-user-database-and-adding-access-on-postgresql-8bfcd2f4a91e

➡️https://gist.github.com/ibraheem4/ce5ccd3e4d7a65589ce84f2a3b7c23a3