0

I just deployed a flask app to EC2 AWS, and when trying to initialise a sqlite db by using the db.create_all(bind=['layout']) method, an error was shown

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file

Here is my code

application.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://{}:{}@{}/{}".format(os.getenv('DB_USER'), os.getenv('DB_PASSWORD'), os.getenv('DB_SERVER'), os.getenv('DB_NAME'))
application.config['SQLALCHEMY_BINDS'] = {
    'dashboard': "postgresql://{}:{}@{}/{}".format(os.getenv('DB_USER'), os.getenv('DB_PASSWORD'), os.getenv('DB_SERVER'), os.getenv('DB_NAME')),
    'layout': "sqlite:///layout.db"
}

I ran db.create_all(bind=['layout']) in the python shell within the ec2 terminal, and I am not sure why I ran into the error, as it worked perfectly fine on my localhost.

The postgresql database works fine, and can be accessed properly, but the sqlite database, which is the one im trying to initialise with db.create_all(bind=['layout']) leads to the error shown above, and I am not sure why it isnt creating a 'layout.db' file within the current directory.

I am new to deployment, and am not sure if I did anything wrong, do let me know if I configured anything wrongly, thank you!

1 Answer 1

1

"sqlite:///layout.db" may end up pointing to a place where your applications doesn't have permission to write. Instead try something like

basedir = os.path.abspath(os.path.dirname(__file__))
...
layout': 'sqlite:///' + os.path.join(basedir, 'layout.db')
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for responding, is it possible for me to use the 'sudo' command along with the running of the db.create_all() command somehow so that i can 'gain the permissions'?
You could, but it's risky

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.