How to create a function in PostgreSQL like SQL Server BACKUP DATABASE TO DISK

Viewed 29

I'm trying without success to create a function in Postgres that save a table or database taking one or two parameters. In this case I was trying to create it with only one parameter(name of the table or database) and backup this table/db

--SELECT backup_table(sports)

CREATE FUNCTION backup_table(TEXT) RETURNS BOOLEAN AS 
$$
DECLARE
    table_x ALIAS FOR $1;
    
BEGIN
    COPY table_x FROM 'C:/path/backup_db' WITH (FORMAT CSV);
    RAISE NOTICE 'Saved correctly the table %',$1;
    

RETURN BOOLEAN;
END;
$$ LANGUAGE plpgsql;

I've always receive the error when I try to execute the function SELECT backup_table(sports): "The column sports doesnt exists." SQL state: 42703 Character: 21

The idea is to create the function like the equivalent of SQL Server BACKUP DATABASE TO DISK, or equivalent to pg_dump command

pg_dump -U -W -F t sports > C:/path/backup_db;

I know about SQL but now I'm just stuck with this error.

0 Answers
Related