Search code examples
pythonpostgresqlflaskflask-sqlalchemyflask-testing

change Test database From sqlite to Postgresql


In my flask application I used Sqlite for Test Now I want to change it to postgresql But Got error OperationalError: (OperationalError) FATAL: database "mytest" does not exist None None am I doing it Right? where is the problem , Thanx

config.py:

class TestingConfig(Config):
    DEBUG = True
    TESTING = True
    PRESERVE_CONTEXT_ON_EXCEPTION = False
    SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL') or \
    os.environ.get('DATABASE_URL','postgresql+psycopg2://admin:password@localhost/mytest')
    print SQLALCHEMY_DATABASE_URI

Solution

  • As the error says, there is no database called "mytest" on the PostgreSQL server you're pointing to. Unlike SQLite which will create a file for the database if it doesn't exist, with PostgreSQL you need to create the database on the server before using it. Assuming PostgreSQL is otherwise set up properly, from the terminal (not the Python shell) run:

    createdb mytest