简体   繁体   中英

Concate string path in psql command line

I have to merge path directory and actual time in psql command line. This absolute path works well:

\copy (select row_to_json(row(column1,column5)) from testdatabase.test) TO 'C:\Users\path\file.json';

But when I want merge two string like this:

\copy (select row_to_json(row(column1,column5)) from testdatabase.test) TO 'C:\Users\path\' || to_char(now(), 'YYYY_MM_DD');

I get Permission denied. This is permission issue i think because first request works well on same path. I tried also CONCATE statement but it fails.

If using COPY directly is an option, you could dynamically build the command, including your custom path logic and EXECUTE it. For example in an anonymous DO block:

DO
$$
BEGIN
  EXECUTE 'COPY (SELECT row_to_json(...) ...) TO ''C:\...\' || to_char(now(), 'YYYY_MM_DD') || '.json''';
END;
$$
LANGUAGE plpgsql;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM