Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
Create a PostgreSQL function from the command line

I'm trying to create a function that allows me to perform multiple Queries in my database. It's the first time that I try to use the command line for this objective. I'm working in a linux system (Ubuntu), using the PostgreSQL.

My Pgsql:

create function my_function(idteste integer, nome character varying, tipo integer, morada character varying, cpostal character varying, telefone character varying, nif character varying, nacionalidade character varying, iban character varying, moradaimag character varying, cc1 character varying, ccimage1 character varying, ccimage2 character varying, datanascimento date)
returns character varying as $tempvar$
declare tempvar character varying;
begin
tempvar := 'null';
if (select count(id_teste) from singulares where id_idteste=idteste) = 0 then
update pessoas set  
nome_pessoal=nome,
tipo_pesoal = tipo,     
morada_pessoal=morada,
cpostal_pessoal=cpostal,
telefone_pessoal=telefone,
nif_pessoal=nif,
nacionalidade_pessoal=nacionalidade,
iban_pessoal=iban,
cmorada_img=cmoradaimag
where
id_teste=idteste;
insert into singulares (pessoas_idteste, cc, cc_img1, cc_img2, data_nascimento) values(idteste, cc1, ccimage1, ccimage2, datanascimento);
tempvar := 'gravado com sucesso';
else
update pessoas set  
nome_pessoal=nome,
tipo_pesoal = tipo,     
morada_pessoal=morada,
cpostal_pessoal=cpostal,
telefone_pessoal=telefone,
nif_pessoal=nif,
nacionalidade_pessoal=nacionalidade,
iban_pessoal=iban,
cmorada_img=cmoradaimag
where
id_teste=idteste;
update singulares set 
cc=cc1,
cc_img1=ccimage1,
cc_img2=ccimage2
where 
pessoas_idteste=idteste;
tempvar := 'alterado com sucesso';
end if;
return tempvar; 
end
$tempvar$  language plpgsql; 

My first question is how can I create the function through the command line?

Second question when creating the function, will this be visible from PGmyadmin?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Assuming your function is stored in a script file create_my_function.sql and you're to create it in database testdb owned by user testuser, here's one way to create it:

psql -h localhost -U testuser -d testdb -f create_my_function.sql

Not sure I understand your second question, but once the function is created it should be visible in any working db admin tool.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!