Dmitri Pisarev
Dmitri Pisarev

Reputation: 1173

PDO SQLite create database default permissions

When using PDO sqlite PHP adapter, new sqlite database file is created with group permission set to read-only, and does not honor umask.

I need every database file to be writable by group. Any way to accomplish it?

Edit: I know how to change permissions of a file, I'm asking whether it is possible to create it with correct permissions (according to process umask) or not.

Upvotes: 3

Views: 2107

Answers (2)

betterworld
betterworld

Reputation: 264

Before opening the database file using sqlite's connect call, you can open is as an ordinary file and the desired permissions. This will create an empty file unless the file is already present. Sqlite will fill that empty file with the database.

This is safer than using chmod after the file has been created for two reasons:

  • There is a tiny possibility that another user could open the file before you call chmod
  • In some cases (e.g. when configured for WAL), sqlite will create more than one file. It will honor the permissions of the pre-existing empty file and use them for the other files. Using chmod on the main file would forget to set permissions for the other files.

Upvotes: 1

Dmitri Pisarev
Dmitri Pisarev

Reputation: 1173

It looks like there's a SQLITE_DEFAULT_FILE_PERMISSIONS compilation parameter.

It seems umask is applied on top of it to restrict it, if necessary. So it makes sense to recompile with SQLITE_DEFAULT_FILE_PERMISSIONS=666 and than rely on umask. Too bad it's not a default option in sqlite.

Upvotes: 3

Related Questions