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)

Subscribe to my spam free newsletter for other Laravel and Vue updates like this

I never spam, and only email when I have a good in-depth post published on my site (mostly about Laravel and Vue). You can also follow me on social media to get updates.

webdevetc profile pic

I've been working as a software developer for many years (with the last few years specialising in Laravel and Vue). I mostly write about PHP and JavaScript/TypeScript on this site. Contact me here. Need to hire a contract software developer in London, UK (or freelance)? Contact me and check my availability.

Leave a Comment