| {% include anchor.html edit="true" title="Create a database" hash="create_database" %} |
| |
| {% highlight js %} |
| new PouchDB([name], [options]) |
| {% endhighlight %} |
| |
| This method creates a database or opens an existing one. If you use a URL like `'http://domain.com/dbname'`, then PouchDB will work as a client to an online CouchDB instance. Otherwise it will create a local database using [whatever backend is present](/adapters.html). |
| |
| ### Options |
| |
| * `name`: You can omit the `name` argument and specify it via `options` instead. Note that the name is required. |
| |
| **Options for local databases:** |
| |
| * `auto_compaction`: This turns on auto compaction, which means `compact()` is called after every change to the database. Defaults to `false`. |
| * `adapter`: One of `'idb'`, `'leveldb'`, `'websql'`, or `'http'`. If unspecified, PouchDB will infer this automatically, preferring IndexedDB to WebSQL in browsers that support both (i.e. Chrome, Opera and Android 4.4+). |
| * `revs_limit`: Specify how many old revisions we keep track (not a copy) of. Specifying a low value means Pouch may not be able to figure out whether a new revision received via replication is related to any it currently has which could result in a conflict. Defaults to `1000`. |
| |
| **Options for remote databases:** |
| |
| * `ajax`: (Remote databases only.) Ajax requester options. For instance, passing in the options `{ajax: {timeout: 10000}}` will allow you to set the max timeout for an HTTP request. These are passed verbatim to [request][] (in Node.js) or [a request shim](https://github.com/pouchdb/pouchdb/blob/master/packages/node_modules/pouchdb-ajax/src/request-browser.js) (in the browser), with the exception of: |
| * `ajax.cache`: Appends a random string to the end of all HTTP GET requests to avoid them being cached on IE. Set this to `true` to prevent this happening. |
| * `ajax.headers`: The `ajax.headers` option allows you to customise headers that are sent to the remote HTTP Server. |
| * `auth.username` + `auth.password`: You can specify HTTP auth parameters either by using a database with a name in the form `http://user:pass@host/name` or via the `auth.username` + `auth.password` options. |
| * `ajax.withCredentials`: Set to `false` to disable transferring cookies or [HTTP Auth information](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials). Defaults to `true`. |
| * `skip_setup`: Initially PouchDB checks if the database exists, and tries to create it, if it does not exist yet. Set this to `true` to skip this setup. |
| |
| **IndexedDB-only options:** |
| |
| * `storage`: Specifies whether you want to use `persistent` or `temporary` storage. This non-standard feature has been supported in Firefox since version 26. Find out more about the available storage types, and how Firefox handles client-side data storage, at [Browser storage limits and eviction criteria](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Browser_storage_limits_and_eviction_criteria). |
| |
| **WebSQL-only options:** |
| |
| * `size`: Amount in MB to request for storage, which you will need if you are storing >5MB in order to [avoid storage limit errors on iOS/Safari](/errors.html#not_enough_space). Persistent views use a separate database per view which will also request storage space so keep in mind the upper storage limits when using persistent views on iOS. |
| |
| **Notes:** |
| |
| 1. In IndexedDB and WebSQL, PouchDB will use `_pouch_` to prefix the internal database names. Do not manually create databases with the same prefix. |
| 2. When acting as a client on Node, any other options given will be passed to [request][]. |
| 3. When using the `'leveldb'` adapter (the default on Node), any other options given will be passed to [levelup][]. |
| |
| [request]: https://github.com/mikeal/request |
| [levelup]: https://github.com/rvagg/node-levelup |
| [levelup_options]: https://github.com/rvagg/node-levelup/#options |
| |
| #### Example Usage: |
| {% highlight js %} |
| var db = new PouchDB('dbname'); |
| // or |
| var db = new PouchDB('http://localhost:5984/dbname'); |
| {% endhighlight %} |
| |
| Create a PouchDB that explicitly uses WebSQL: |
| |
| {% highlight js %} |
| var db = new PouchDB('dbname', {adapter : 'websql'}); |
| {% endhighlight %} |
| |
| Create an in-memory Pouch (must install `pouchdb-adapter-memory` first): |
| |
| {% highlight js %} |
| var db = new PouchDB('dbname', {adapter: 'memory'}); |
| {% endhighlight %} |
| |
| Create a remote PouchDB with special Ajax options: |
| |
| {% highlight js %} |
| var db = new PouchDB('http://example.com/dbname', { |
| ajax: { |
| cache: false, |
| timeout: 10000, |
| headers: { |
| 'X-Some-Special-Header': 'foo' |
| }, |
| }, |
| auth: { |
| username: 'mysecretusername', |
| password: 'mysecretpassword' |
| } |
| }); |
| {% endhighlight %} |
| |
| For more info, check out [adapters](/adapters.html). |