Search code examples
macosdeploymentpackaging

Best location for database file in Mac OSX


I'm working on an installer for an application that I have ported to OSX, but I don't have much experience with this OS so I want to know where is the most appropriate location to install a database file that will be used by my app.

The database file is relatively small, and will be updated by a daemon process but must be accessible to all users on the system via a command-line tool. On Linux I have used /var/lib/myapp/mydata.db, what would be the OSX equivalent?


Solution

  • There are two approaches on OS X:

    1. OS X defines application and user-level "application support" folders. These are in /Library/Application Support for system-wide and in ~/Library/Application Support/ for user-level support files. Application support folders are supposed to be named by the app's bundle identifier (e.g. com.company.app), but many just use the application name. In general, OS X uses this pattern (system-level in /Library and user-level in ~/Library for application support, preferences, plugins, etc.). /Library is, by default read-only for non-administrator users. If your command line tool is run as root (or via sudo), then you could put your database file in /Library/Application Support/your-app. Of course, since you'll need to do this in an installer script (either traditional unix style or via an OS X installation package), you could modify the read-write permissions of your db file.

    2. As others have pointed out, OS X is a UNIX OS, so you can create /var/lib or any other folder to your liking. Many UNIX ports do this as it keeps things more consistent with other OSes that they support. If you do this, make sure to provide an unistaller script that removes the folders you add (assuming nothing else has been added to them by other apps/users/etc.!). Mac users generally frown on installers throwing crap all over the file system without an easy way to clean it up. Also, MacPorts, and Fink (two OSS package managers for OS X) use the /opt and /sw trees respectively. You probably don't want to mess with those trees as it could cause problems for users of these package managers. Of course, one of the best ways to go if you want to keep things UNIX-y is to package your app for MacPorts or Fink. MacPorts is of the BSD ports tree lineage and Fink is of the debian lineage. MacPorts is the Apple-sanctioned package manager, but both seem popular.