2

I want to create a directory in PL/pgsql:

CREATE OR REPLACE FUNCTION getNextId() RETURNS VARCHAR(200) AS
$BODY$
DECLARE
 mfId integer;
 rootDir varchar(50) := 'mfdata/';
 createDir text := 'mkdir --mode=777 -p mfdata';
 dir varchar(100);
BEGIN
 INSERT INTO copy.history(time) values(now()) RETURNING id INTO mfId;
 EXECUTE('COPY (SELECT 1) TO PROGRAM ' || quote_literal('mkdir --mode=777 -p mfdata '));
 RETURN 'mfdata' || mfId;
END; 
$BODY$ 
LANGUAGE plpgsql;

but occur an error as follow:

enter image description here

Where is the problem? How can I create a directory dynamically in PL/pgSQL?

asked Jan 24, 2018 at 7:46
1
  • 1
    you should write more about your task and what you're trying to do Commented Jan 25, 2018 at 16:43

1 Answer 1

1

It looks good. There might be two issues when using COPY {FROM | TO } PROGRAM though:

  1. missing permissions on the directory you want to create the new one in. With your present code this is unlikely, as your dir will be created in the Postgres data directory. The directory would be created as owned by the OS user/group postgres, permissions set to 700.
  2. you are trying this as a non-superuser. That won't work - and also produces a different error:
ERROR: must be superuser to COPY to or from an external program
HINT: Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone.

Based on the above, your problem is the first case. Check if the user has write permission on the target directory.

answered Jan 24, 2018 at 10:43
1
  • 1
    Extra bold on ** directory would be created as owned by the OS user/group postgres, permissions set to 700.** that's his problem. Commented Jan 25, 2018 at 16:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.