With sqlite one can easily populate a table in a database via a org-table like so:
[EDIT](using only one sqlite-scr-block with org-table variable parameter)[/EDIT]
#+NAME: A
| val1 | val2 |
|------+------|
| 2 | 16 |
| 4 | 25 |
| 10 | 2 |
#+begin_src sqlite :db */path/to/db* :var orgtable=A :colnames yes
drop table if exists A;
create table A(val1 int, val2 int);
.mode csv A
.import $orgtable A
select * from a;
#+end_src
#+RESULTS:
| val1 | val2 |
|------+------|
| 2 | 16 |
| 4 | 25 |
| 10 | 2 |
I want to do this with postgresql. I tried with some COPY
commands, but did not find other concrete info, on how to do this with org-variables.
How can I populate a table in postgresql with the contents of an org-mode-table?
EDIT (after @Martín's answer and @NickD's comment): I "merged" the answer and the comment.
1) Export table to tmp-file
#+begin_src emacs-lisp :var orgtable=A
(write-region (orgtbl-to-csv orgtable nil) nil "/tmp/test.csv")
#+end_src
2) Import to db via localcopy \copy
in sql-src-block
#+name: my-query
#+header: :engine postgresql
#+header: :dbhost localhost
#+header: :dbuser USER
#+header: :dbpassword PW
#+header: :database org-db
#+begin_src sql
drop table if exists A;
create table A(val1 int, val2 int);
\copy A(val1,val2) FROM '/tmp/test.csv' DELIMITER ',';
#+end_src
This comes quite close… But before I start to improve this, I'd love hints and answers how to do this with variable as input (w/o the detour with 2 src-blocks and a transfer tmp file).