CouchDB - The Open Source NoSQL Database
August 30, 2010
CouchDB is a great example of forward-thinking technology that's worth a look even if you're not currently in need of a database solution. This article introduces you to CouchDB, showing you how to get started using this fascinating solution and even integrate it into your PHP website.
In a recent Developer.com article I introduced MongoDB, a NoSQL database which is experiencing widespread adoption within enterprises such as github and The New York Times. If you're new to the technology, NoSQL is a generic term for a database which relies upon a key-value storage solution rather than a formally defined schema, with the goal of providing developers with increased flexibility, availability, and scalability not easily obtained with traditional relational databases.
Although among the most popular, MongoDB is just one of many open source NoSQL solutions in widespread use today. Another popular solution is CouchDB, which like MongoDB is such a great example of forward-thinking technology that it's worth a look even if you're not currently in need of a database solution! In this article I'll introduce you to CouchDB, showing you how to get started using this fascinating solution and even integrate it into your PHP website.
Django developer Jacob Kaplan-Moss summed up CouchDB better than any other definition I've encountered so far, stating, "Django may be built for the Web, but CouchDB is built of the Web. I've never seen software that so completely embraces the philosophies behind HTTP."
You'll interact with CouchDB data using HTTP's request verbs, including DELETE, GET, POST, and PUT, using these requests to send and receive JSON-formatted data. This means it's possible to use all of today's mainstream programming languages in conjunction with CouchDB, including Java, PHP, Python, Perl, and Ruby. Later in this article I'll show you just how easy it is to plug CouchDB into your PHP-driven website.
CouchDB is supported on all of the major platforms. If you're running a *nix-based system, then chances are quite good that you can install CouchDB via your distribution's package manager. For instance, on Ubuntu installing CouchDB was as easy as using
You can find a comprehensive list of installation instructions on the CouchDB wiki.
Interacting with CouchDB
Once installed and configured, CouchDB will listen for requests on port 5984. Yet unlike a database such as MySQL (which incidentally runs on port 3306), remember that CouchDB is HTTP-driven, meaning you can contact the CouchDB server by opening a browser and pointing it to
You might recognize this response format as a simple JSON string which contains two key-value pairs. Having successfully contacted the server, let's next create a database. To do this, you'll use a PUT request. Because it's not possible to send a PUT request from the browser address bar, we'll use cURL. cURL is a command-line tool (and library) useful for transferring data using a variety of protocols. If you're running any *nix-based system, it's likely already installed. Windows users can download a version from here.
Once you've confirmed that cURL is installed, open up a terminal and execute the following command:
Yes that's right, anybody who can access this URL can create a new database, as well as perform other administrative tasks. CouchDB does however support the ability to create administrator accounts and restrict access to administrative tasks. You can learn more about this topic here.
As indicated by the response, a database named
In this example we added a new market, including its name, address, city, and telephone. But what if a particular farmer's market didn't specify a contact? The beauty of document-based databases is that you simply omit it:
In both examples notice how an identifier was returned. You can use these identifiers to uniquely retrieve each row (formatted for readability):
Incidentally, CouchDB is bundled with a simple Web-based interface called Futon which you can use to peruse and manage your databases. To access this interface, navigate to
While using cURL and Futon is fine for getting acquainted with CouchDB, you'll likely soon want to begin interacting with the database using a programming language such as PHP. I'll show you how to do this in the next section.
Talking to CouchDB with PHP
Although PHP doesn't yet offer a native CouchDB extension, several third-party libraries are available, including PHPillow (as in couch pillow, get it?). To use PHPillow you'll need to be running PHP 5.2+. Presuming you meet this requirement, head over to the PHPillow download page to retrieve the latest version. Download and uncompress the code and place the directory somewhere within PHP's include path.
Next create a class which we'll use to represent the markets. This class should extend PHPillow's abstract
This example defines several required class structures you'll need to create when using PHPillow. The
With the class in place, you can use it to add a new market:
Executing this example will save the new market, and return its unique ID:
Notice how the type is used as a prefix to the unique title. You can use this unique ID to retrieve the document:
Executing this snippet produces the address:
CouchDB is another best-of-breed solution in the cutting-edge NoSQL database market. Sporting a modern architecture, backing by the Apache Software Foundation, and active support within all of the popular programming language communities, CouchDB is well worth a look if you're interested in migrating to a document-oriented database solution. Are you using CouchDB or another NoSQL solution? Tell us about it in the comments!
About the Author
Jason Gilmore is founder of the publishing and consulting firm WJGilmore.com. He is the author of several popular books "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Third Edition". Follow him on Twitter at @wjgilmore.