Search code examples
postgresqlpsql

psql function written in separate file


i'm learning psql in Postgres. My basic question is when i create a function like this one :

CREATE OR REPLACE FUNCTION totalRecords ()
RETURNS integer AS $total$
declare
    total integer;
BEGIN
  SELECT count(*) into total FROM COMPANY;
  RETURN total;
END;
$total$ LANGUAGE plpgsql;

i must write all of the code in the prompt command line. How can i save this code in a script and call it from the command line ? the extentsion of the scirpt must be a .sql ? how can i call this script.


Solution

  • Save your script to a file. Then execute like this:

    psql -p portnumber -d database -U user -f mysqlscrpt.sql 
    

    The extension of the script does not matter.