windows 10, emacs 26.1

I know that in org mode I can insert source code and execute them. Nice.

But what about sql:

Here in my.org file:

 #+name: some_query
   #+BEGIN_SRC sql
      SELECT * FROM invoice WHERE id IN (1,2,3)

Is it possible to execute query direct in org mode and get result?

  • 3
    Yes: there is ob-sql.el and ob-sqlite.el. The latter is specific to sqlite but the former can handle different database engines. Read the beginning of ob-sql.el to see how to use it and check out also the SQL doc on Worg.
    – NickD
    Nov 29, 2018 at 17:18

1 Answer 1


Yes. ob-sql is what I would use. Link to documentation with examples: https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-sql.html

You can specify your connection credentials directly as parameters of the source block:

#+begin_src sql :engine postgresql :dbhost HOSTNAME :dbuser USERNAME :dbpassword PASSWORD :database DBNAME

The results are formatted as org-mode table in RESULTS block below after execution.

You could also configure the credentials under a header with (source):

 * some header that the sql source block will be in
  :header-args+: :results table
  :header-args+: :engine mssql
  :header-args+: :dbhost
  :header-args+: :database database-name-a
  :header-args+: :dbuser username-a
  :header-args+: :dbpassword password-a

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.