The Beginner’s Guide to PouchDB

DZone 's Guide to

The Beginner’s Guide to PouchDB

PouchDB is an open-source database API with powerful features. It supports all modern browsers, can build various types of applications, and more.

· Database Zone ·
Free Resource

PouchDB is an open-source client-side database API written in JavaScript. It is modeled after CouchDB, a NoSQL database that runs within the browser.

If you are not familiar with NoSQL databases, let me give you a short introduction. NoSQL is a term used for high-performance, non-relational databases. It provides a mechanism for storage and retrieval of data other than tabular relations model used in relational databases. These databases utilize a variety of data models, including document, graph, key-value, and column store. Check out the list of NoSQL databases here.

Using the PouchDB API, we can build applications that work offline and online. It uses WebSQL and IndexedDB internally to store the data. It is framework-agnostic, so you can use it with Angular, React, Ember, Backbone, or your framework of choice.

PouchDB supports all the modern browsers. Some of them are Firefox 29+, Chrome 30+, Safari 5+, Internet Explorer 10+, Opera 21+, and so on.

As we know PouchDB can be used as a direct interface to CouchDB-compatible servers. So, let’s set up CouchDB.

Installing CouchDB

If you are using one of the Linux distributions, you can install using the following command:

$ sudo apt-get install couchdb

For Windows, download and install from the CouchDB site.

If you're using a Mac, you can run the following command in the terminal:

$ brew install couchdb

Once it is installed, you can open or http://localhost:5984/ in the browser. The response should be something similar to as follows:

{"couchdb":"Welcome","version":"1.6.1", .. }}

When you open, you will see a screen as follows.

Image title

Note: On Mac, if you do not get any response when you open in the browser, verify whether the CouchDB service is running using the command:

$ curl

If you get the following response, it's probably not running:

curl: (7) Failed to connect to port 5984: Connection refused

In that case, run the following command to start the service:

$ couchdb 

Setting Up CORS

To create a PouchDB database in a remote CouchDB server, we will need to enable CORS in CouchDB.

Install CORS using the following script:

$ npm install -g add-cors-to-couchdb

Then run the script:

$ add-cors-to-couchdb

Installing PouchDB

Download the latest pouchdb-6.3.4.min.js from their website and include in index.html as follows:

<script src="pouchdb-6.3.4.min.js"></script>

If you are using Node.js, you can run the command as follows:

$ npm install pouchdb

Refer this documentation for other various ways to install PouchDB.

Creating a PouchDB Database

To create a local database in PouchDB using a node, we can simply write the following code snippet and save it as index.js.

//Requiring the module
let pouchDB = require("PouchDB");

//Creating the database object
let db = new PouchDB("techshard");
console.log ("Database created Successfully.");

Then run the command as follows:

$ node index.js

Image title

To create a remote database, you will need to provide remote server URL.

//Requiring the package

let PouchDB = require("PouchDB");

//Creating the database object

let db = new PouchDB("");

db.info().then(function (info) {



Here, the remote database will not be created until we do an API call db.info(). Once you run the above JavaScript, you should be able to view the info as follows:

Image title

We can also view the information by opening in the browser.

So far, we learned how to install and setup PouchDB. Let’s look at some database operations.

Storing and Reading a Document

Since PouchDB is a NoSQL database, we store unstructured documents. For example:

let doc = {

   _id : '01',

   name: 'John Doe',

   age : 24,

   designation : 'Developer'



This will store the document in the database. To read the same document, we can write as follows:

db.get('01').then(function (doc) {



Here, ‘01’ is an _id of the document.

We will see the following output in the console:


_id: '01',

  _rev: '1-7624dcaea7247ee0c6e280f8482d4ee7',

  name: 'John Doe',

  age: 24,

  designation: 'Developer'


Here, the new field _rev is the revision marker. It is a randomly generated ID that changes whenever a document is created or updated.

Deleting a Document

For deletion, we can simply fetch the document and delete the returned document.

db.get('01').then(function (doc) {  

return db.remove(doc);


There are other ways to delete a document in PouchDB API. Please refer this documentation for more information.

Wrapping Up

PouchDB has a lot more powerful features. If you are interested in learning in depth, check out their official site for detailed documentation and API references. I hope this article has sparked your interest in PouchDB. Let me know in the comments if you already have experience with CouchDB or PouchDB.

api, couchdb, database, pouchdb, tutorial

Published at DZone with permission of Swathi Prasad , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}