I'm using org-babel to do litterate programming and I find it very convenient. I am able to execute shell commands on remote hosts, and local database commands with the postgres client. To open a file as postgres user, it would look for example like
C-x C-f /sshx:user@remotehost|sudo:postgres@remotehost:/tmp/testfile
To do so on Emacs 24.5.1, I needed to add:
(require 'tramp)
(add-to-list 'tramp-default-proxies-alist
'(nil "\\`postgres\\'" "/ssh:%h:"))
(add-to-list 'tramp-default-proxies-alist
'((regexp-quote (system-name)) nil nil))
So far, I have failed to understand how one could execute a postgresql (or mysql if you so choose) command on a remote server. The database server is not accessible directly over the network, I have to ssh into the remote host, su as the postgres user and then I'm able to execute commands through the psql client.
How could one use such a command:
#+BEGIN_SRC sql :engine postgresql :ExtraParametersIfNeeded
SELECT * from pg_database
#+END_SRC
On a remote host? I know the :dir switch, but have so far only been able to use it for shell scripts.
Many thanks!
ssh yourbox -L7590:127.0.0.1:7590
" replacing the "7590
" with whatever port your database is on. Then, in your Org babel block, you can use the ":cmdline
" header argument to set a command line argument to set the database port. The whole thing might look something like this at the top of your Org file: "#+PROPERTY: header-args:sql :engine mysql :dbhost 127.0.0.1 :dbuser someone :dbpassword somepassword :database database :cmdline -P7590
". AfterC-c C-c
-ing that, you can use any SQL block in your buffer like that.