Postgresql – Wildcard for meta-command in postgresql

copycsvpostgresqlpsql

I am searching if there is a wildcard assigned to the \ meta-commandsSee description – meta-commands in Postgresql.

for example..

postgresql=> \copy scheme.table FROM 'D:\path\to\%.csv' DELIMITER ';' CSV ENCODING 'LATIN1';

Make notice of the % in the example provided – A known declaration of wildcards could be _, %, . or *. But I do not seem to find documentation of wildcards in /meta-commands for Postresql.

Best Answer

First \COPY isn't postgresql. That's the client psql. From the docs,

Do not confuse COPY with the psql instruction \copy. \copy invokes COPY FROM STDIN or COPY TO STDOUT, and then fetches/stores the data in a file accessible to the psql client. Thus, file accessibility and access rights depend on the client rather than the server when \copy is used.

No, psql does not provide filename completion. However there are other wrapper around COPY FROM STDIN that are not psql and they do. For instance pgloader provides FROM [ ALL FILENAMES | [ FIRST ] FILENAME ] MATCHING

The same clause can also be applied to several files with using the spelling FROM ALL FILENAMES MATCHING and a regular expression. The whole matching clause must follow the following rule:

And on top of that you can always use bash or whatever shell you're using to launch psql. Example with bash filename expansion.

for i in *.csv;
  do psql -c "\copy scheme.table FROM $$D:\path\to\$i.csv$$ DELIMITER ';' CSV ENCODING 'LATIN1';"
done;

Power-shell certainly also provides filename expansion. Google for it if you're on Windows 8+.