I'm working with org-mode and org-babel to query a PostgreSQL database. It's pretty cool setup and I love it.

At the moment, I have multiple org files with the same database property configuration like the following.

#+PROPERTY: header-args:sql :dbhost myhost
#+PROPERTY: header-args:sql+ :dbuser username :database secretdb :engine postgresql :exports result

This is duplicated in each org file now. I'm wondering if there's a way to configure this in one place and org-babel pick it up from.

I also have database configuration for multiple environments. So configuration for each database is also duplicated in the respective org files. It was fine in the beginning, now I have too many files with same +#PROPERTY: value in them, and it kind of getting confusing and tedious.

I'm looking for a way to have the configuration in one place and org-babel have them pickup.

Here's is what I tried so far.

  1. #+INCLUDE: "./connection-properties.org"

You get the idea. This includes the org file, and also says Local setup has been refreshed when I do Ctrl-C-Ctrl-C. But when I execute the sql block, it fails with org-babel-execute:sql: Wrong type argument: stringp, nil

Looking for clues! Thanks!


I did it! Yaay!

I learned about org-global-properties here https://orgmode.org/manual/Property-syntax.html and gave a shot by setting in it in .dir-locals.el file. It works exactly as I wanted!

I have a .dir-locals.el file in each directory that represents and environment.

├── prod │   ├── .dir-locals.el │   └── database │   └── 01-queries.org │   └── 02-queries.org ├── staging │   ├── .dir-locals.el │   └── database │   └── 01-queries.org │   └── 02-queries.org ├── uat │   ├── .dir-locals.el │   └── database │   └── 01-queries.org │   └── 02-queries.org ├── localhost │   ├── .dir-locals.el │   └── database │   └── 01-queries.org │   └── 02-queries.org

.dir-locals.el file works like magic!

And here is the content of .dir-locals.el file.

((org-mode . (
              (org-global-properties . (
                                        (header-args:sql . ":dbhost host :database dbname :engine postgresql :exports result")

Instead of using :dbpassword in org-global-properties, I set it in ~/.pgpass file.

This serves my purpose, but if you have any suggestions for improvements please comment or give a separate answer. Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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