Laravel + SQLite - works in php artisan tinker, but gives "unable to open db file" - FIXED

Laravel + SQLite - works in php artisan tinker, but gives  "unable to open db file" - FIXED Thumbnail image

This is just a quick post, maybe it'll help someone who searches for this. I'll try and stuff a few keywords in the post so it'll come up if someone searches for this problem :)

I was writing a small app in Laravel, that used SQLite, in /database/database.sqlite. I rarely use SQLite - but this was a tiny app that needed to be moved around servers so it made perfect sense.

It worked fine locally. I transferred to another server, and the migration (php artisan migrate:fresh) worked (so the database was writable/was working). However when I used a browser to access to the app, it would give errors like General error: 14 unable to open database file (other errors given at bottom of this post).

I had the permissions set up correctly for this file. This wasn't a public facing service, and I was running out of ideas. I even tried chmod 777 ./database/database.sqlite. I tried changing the owner of the .sqlite file. Nothing helped.

The fix was simple. As this comment points out:

If you receive an error while trying to write to a sqlite database (update, delete, drop):

Warning: PDO::query() [function.query]: SQLSTATE[HY000]: General error: 1 unable to open database

The folder that houses the database file must be writeable.

A simple chown of the actual /database/ directory fixed my problems.

(Writes to the SQLite database were working fine in php artisan migrate or php artisan tinker because it was a different user than what the (apache) web server was using).

(Some other errors that might be because of this problem include: [PDOException] SQLSTATE[HY000] [14] unable to open database file, or SQLSTATE[HY000]General Error: 8 attempt to write a readonly database)

webdevetc profile pic
webdevetc

I am a 29 year old freelance backend web developer from London, mostly focusing on PHP and Laravel lately. This is my site - I mostly write about PHP here. Contact me here (especially for any contracting jobs early 2019 in London ;) ).

Leave a Comment