# Keeping two v2 separate databases

**URL:** <https://org-roam.discourse.group/t/keeping-two-v2-separate-databases/1976>\
**Category:** How To\
**Created:** [August 25, 2021, 3:17pm UTC](https://org-roam.discourse.group/t/keeping-two-v2-separate-databases/1976 "2021-08-25T15:17:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmburgos](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/jmburgos/32/261_2.png) [@jmburgos](https://org-roam.discourse.group/u/jmburgos)\
**Post date:** [August 25, 2021, 3:17pm UTC](https://org-roam.discourse.group/t/keeping-two-v2-separate-databases/1976/1 "2021-08-25T15:17:20Z")

</div>

Hello everyone,

I would like to keep two separate org-roam v2 databases (work and personal stuff), and have a simple way to switch back and forth between them. Before I reinvent the wheel and write some elisp code, does anybody has a good way to do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![nobiot](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/nobiot/32/159_2.png) [@nobiot](https://org-roam.discourse.group/u/nobiot)\
**Post date:** [August 25, 2021, 6:41pm UTC](https://org-roam.discourse.group/t/keeping-two-v2-separate-databases/1976/2 "2021-08-25T18:41:48Z")

</div>

This?  
[https://www.orgroam.com/manual.html#How-do-I-have-more-than-one-Org\_002droam-directory\_003f](https://www.orgroam.com/manual.html#How-do-I-have-more-than-one-Org_002droam-directory_003f)

It seems to be working [V2 backlinks query does not find local database when running multiple org-roam environments · Issue #1482 · org-roam/org-roam · GitHub](https://github.com/org-roam/org-roam/issues/1482)

---

<div class="post-metadata">

**Author:** ![jmburgos](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/jmburgos/32/261_2.png) [@jmburgos](https://org-roam.discourse.group/u/jmburgos)\
**Post date:** [August 26, 2021, 2:51pm UTC](https://org-roam.discourse.group/t/keeping-two-v2-separate-databases/1976/3 "2021-08-26T14:51:16Z")

</div>

Thank you. Yes, I saw that bit of the documentation but I am not sure if i understand how this is supposed to work. My first attempt was to do something like this:

```auto

(defun org-roam-swap-databases ()
  
  "Swap between work and personal org roam databases"
  
  (if (equal org-roam-directory "/home/julian/Documents/Notes/")
      (setq org-roam-directory "/home/julian/Documents/Personal/brain/")
      (setq org-roam-directory "/home/julian/Documents/Notes/"))
   (org-roam-db-sync)

   )

```

Which works, but requires that the database is synchronized after swapping, and I am not sure if this is the best do do it.

---

<div class="post-metadata">

**Author:** ![nobiot](https://yyz2.discourse-cdn.com/free1/user_avatar/org-roam.discourse.group/nobiot/32/159_2.png) [@nobiot](https://org-roam.discourse.group/u/nobiot)\
**Post date:** [August 26, 2021, 6:29pm UTC](https://org-roam.discourse.group/t/keeping-two-v2-separate-databases/1976/4 "2021-08-26T18:29:21Z")

</div>

I don’t use .dir-locals.el but the documentation tells me this.

 ![image](https://global.discourse-cdn.com/free1/uploads/orgroam/original/1X/c31490ad991943a315ec282039a2aaaaa197ed3c.png)

My understanding is that when you visit a file under the directory (and its sub-directories), Emacs swaps the variables – it may not be convenient if you want to sync or node-find **before** visiting such a file. But that seems to be the way?

> The usual way to define directory-local variables is to put a file  
> named ‘.dir-locals.el’(1) in a directory. Whenever Emacs visits any  
> file in that directory or any of its subdirectories, it will apply the  
> directory-local variables specified in ‘.dir-locals.el’, as though they  
> had been defined as file-local variables for that file (\*note File  
> Variables::).

---

<div class="post-metadata">

**Author:** ![zbodtorf](https://avatars.discourse-cdn.com/v4/letter/z/919ad9/32.png) [@zbodtorf](https://org-roam.discourse.group/u/zbodtorf)\
**Post date:** [August 27, 2021, 3:26am UTC](https://org-roam.discourse.group/t/keeping-two-v2-separate-databases/1976/5 "2021-08-27T03:26:35Z")

</div>

Perhaps this modified @holtzermann17 snippet will help – I’ve been it in my config for some time now. Any suggestions to refine/improve this for v2? It works well on MacOS but it takes a couple tries to load on Windows.

```auto
#+begin_src emacs-lisp
;; see gist.github.com/holtzermann17 for these two functions for org-roam v1
(defvar org-roam-library
  `(,(concat "/Users/" (getenv "USER") "/Dropbox/org/org-roam/")
    ,(concat "/Users/" (getenv "USER") "/Dropbox/org/scratch/")
    ,(concat "/Users/" (getenv "USER") "/Dropbox/org/journal/")
    ,(concat "/Users/" (getenv "USER") "/Documents/powerbi-docs/")
))
(defun org-roam-checkout ()
  (interactive)
  (let ((ctx org-roam-directory))
    (if (eq (length org-roam-library) 1)
        ;; Still go ahead and set the variable in this case!
        (progn (setq org-roam-directory (car org-roam-library)
                     org-roam-db-location (concat org-roam-directory "org-roam.db"))
               (message "You only have one choice for org-roam-directory defined."))
      (let ((lib (completing-read "Choose a volume: " org-roam-library)))
        (when lib
          (setq org-roam-directory lib
                org-roam-db-location (concat org-roam-directory "org-roam.db")))))
    ;; Assuming that the user changes context, let’s rebuild the cache
    ;; and prompt them to choose a new file in that context
    (when (not (eq ctx org-roam-directory))
      (org-roam-db-sync)
      (org-roam-node-find))))
#+end_src

```
