Skip to content

Using PostgreSQL instead of Sqlite

brandon flowers edited this page Jun 2, 2019 · 7 revisions

A Walkthrough of how I replaced Sqlite with PostgreSQL for Development & Production

Step 1/3

In the app folder, find the requirements.txt and add:

psycopg2==2.8.2

Step 2/3

In the root direction, find local_config.py.template and rename it to local_config.py

Next, find config.py and edit it to reflect postgresql instead of sqlite. I added settings for my local Postgres db used in development and the Postgres db that I'll be using remotely in production.

class BaseConfig(object):
    SECRET_KEY = 'cabinquest-probe'
    ENV = "development"

    PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
 
    # POSTGRESQL - REMOTE 
    DB_USER_REMOTE = 'my-remote-user'
    DB_PASSWORD_REMOTE = 'my-remote-password'
    DB_NAME_REMOTE = 'my-remote-db-name'
    DB_HOST_REMOTE = 'my-host-remote'
    DB_PORT_REMOTE = 5432

    # POSTGRESQL - LOCAL 
    DB_USER_LOCAL = 'my-local-user'
    DB_PASSWORD_LOCAL = 'my-local-password'
    DB_NAME_LOCAL = 'my-local-db-name'
    DB_HOST_LOCAL = 'localhost'
    DB_PORT_LOCAL = 5432

    if ENV == "development":
        SQLALCHEMY_DATABASE_URI = 'postgresql://{user}:{password}@{host}:{port}/{name}'.format(
            user=DB_USER_LOCAL,
            password=DB_PASSWORD_LOCAL,
            host=DB_HOST_LOCAL,
            port=DB_PORT_LOCAL,
            name=DB_NAME_LOCAL,
        )
    else:
        SQLALCHEMY_DATABASE_URI = 'postgresql://{user}:{password}@{host}:{port}/{name}'.format(
            user=DB_USER_REMOTE,
            password=DB_PASSWORD_REMOTE,
            host=DB_HOST_REMOTE,
            port=DB_PORT_REMOTE,
            name=DB_NAME_REMOTE,
        )   
     

    # SQLITE
    # SQLALCHEMY_DATABASE_URI = 'sqlite:///%s' % (os.path.join(PROJECT_ROOT, "example.db"))

Step 3/3

Make sure you have installed invoke

I created a virtual environment called teams

$ python3 -m venv teams
$ source teams/bin/activate
$ invoke app.run 

Then you should be see the demo in the browser

http://127.0.0.1:5000/api/v1/

In order to test the endpoints, we need click the authorize button on top right.

Authorize

Username: root
Password: q 

From the drop down select "Basic Auth" which will reveal 2 more fields:

clientID: documentation
Secret: KQ()SWK)SQK)QWSKQW(SKQ)S(QWSQW(SJ*HQ&HQW*SQ*^SSQWSGQSG

If you only see Cancel, scroll down and click the Authorize button

The screen will flash, if you are connected, instead of Authorize, you should now see Logout. Also now you will be able to test the endpoints.

Clone this wiki locally