22

I don't see an API for renaming a CouchDB database, or an option in Futon to do the same.

Short of replicating to another database and then deleting the original, is there a one-step way to rename a database?

4 Answers 4

30

Adding to what Dominic and PPPaul said...

Renaming the file is absolutely the way to go (make sure you rename it as the same user, not accidentally change the ownership to root or something).

This is perfectly safe to do in a full running production environment, yes. One of the many advantages of an HTTP/REST API is that there are no adapters with stateful connections to the DB. Each request is a new request. So basically the first request to CouchDB after the rename is complete will be able to use the new name.

But wait, there's more!

One really cool thing is that even after the rename, the CouchDB server still has an open file-handle to the file itself (which isn't affected by renaming the inode), so until the CouchDB server is actually restarted you'll still be able to access the DB using the old name too.

Update

Great observation by ATfPT in the comments, apparently the DB name needs to start with a lower case letter, otherwise it doesn't show up in Futon.

Update

Another thing I remembered: to find out where the DB files are, run this: couch-config --db-dir

2
  • 3
    It's too bad there's no API call for this. If you don't have full access to your CouchDB server then this solution is unavailable to you. It seems you would have to replicate and delete, which DOES indeed have the side effect of loosing revision and view index data.
    – fet
    Nov 19, 2012 at 18:45
  • 2
    The databases no longer are listed in Futon using this method, do you have any idea how this could be remedied? EDIT: ignore this, i had renamed them with a capital letter prefix, they must start lowercase. Jan 30, 2014 at 12:05
10

If you have access to the database file itself, (located in /var/lib/couchdb depending on your installation) you can simply rename it.

To my knowledge, that will effectively "rename" the database. I would make sure the server is not running when you perform this operation, just in case.

4
  • Is this safe? I mean doesn't that have some side effects? Jul 21, 2012 at 14:51
  • Honestly, I'm not entirely sure as I've never done this myself in production. But I do know for a fact that simply copying the database files is sufficient for a backup/restore, so a rename is probably safe too. Jul 21, 2012 at 15:00
  • Makes sense, I'll try this out just to make sure. ;) Jul 21, 2012 at 15:18
  • 3
    The path /var/lib/couchdb seems obsolete, I can only see directories .delete, eb1d890cf2a17c53cff7a289065c4dd5, ._replicator_design, shards, .shards, ._users_design and files _dbs.couch, _nodes.couch, _replicator.couch, _users.couch
    – Peter G.
    Feb 9, 2018 at 10:26
6

I am using couchdb 2, i assume the previous answers are for an older version because the database files are not listed in the /var/lib/couchdb folder found using 'couch-config --db-dir' command.

the simplest way i am following is to use futon and replicate the db you want to rename to a new desired name then delete the old database.

1
  • Indeed, with CouchDB v2+ I don't see any other way to "rename" a database. And even with replication I had my problems, it didn't work :(
    – franc
    Jan 18, 2021 at 7:41
0

Assuming you don't want to corrupt your databases, rename the .couch files when CouchDB is not running.

2
  • Actually this isn't a problem, due to CouchDB's append-only design. You can rename and/or copy while CouchDB is running without corruption. (This assumes that your application can tolerate non-atomic writes, of course. But if it couldn't tolerate non-atomic writes, then you shouldn't be using CouchDB in the first place.) Aug 12, 2013 at 20:24
  • 2
    Not safely, couchdb will have an open file descriptor to the file and be unaware of the rename. Oct 18, 2013 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.