Fix the SQLite "attempt to write a readonly database" error

Fix the SQLite "attempt to write a readonly database" error

If you’re encountering "General error - 8 attempt to write a readonly database" when working with SQLite, and confused because the write permission’s set on the file, here’s the solution.

Earlier this week, after deploying a Mezzio application, one that interacts with SQLite, I encountered the following error:

SQLSTATE[HY000]: General error: 8 attempt to write a readonly database

On the surface of it – especially as SQLite is a single disk file a database contained in a single disk file – this seemed like a simple enough issue to solve. The database file’s write bit for the web server user or group wasn’t set. So, after setting it appropriately, the application would work.

Sure enough, on reviewing the file’s permissions, the write bit wasn’t set for either the user nor the group of the file, similar to the example below.

-r--r--r-- 1 webroot webroot 16K May 10 12:35 data/db.sqlite3

On seeing this, I used chmod to set the write bit for both user and group and reloaded the application, expecting to not see the error again. Strangely, the error persisted. Then, just for a laugh, I set the read, write, and execute bits for all users, and reloaded the application. Still, the error persisted.

It just seemed strange that, despite all relevant bits being set on the file, it was still reporting as read-only. So, I reverted the previous update to the file’s permissions and started doing some hunting around.

Low and behold, thanks to a post on Stack Overflow, I found the issue, one which I never expected. Buried in the comments of the PHP manual’s entry for the PDO SQLite driver is the following:

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.

My working knowledge of Linux filesystems is a little rusty, so this seemed strange, but I thought I’d test it all the same. Sure enough, after setting the write bit for the webserver user and group on the enclosing directory and the database file, the error disappeared.

So, if you’ve encountered this issue, remember this, and all will be well.

You might also be interested in these tutorials too...

Thu, Jun 27, 2013

\Zend\Db\Sql - Creating Joins and Unions with Ease

In part two of the \Zend\Db\Sql\Select series, we create inner, outer, left and right joins as well as union queries with ease. Come learn more about building queries simply, effectively and easily in your every day applications with Zend Framework 2.

Learning Golang. Day 3
Wed, May 11, 2022

Learning Golang. Day 3

Here I am at day 3. Today, I started learning about pointers, arrays, slices, and structs!

Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus