Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

358
Visualizações
I/O File Operations in Postgresql

I am brand new to PostgreSQL and I need someone to point me in the right direction on how to write the results of a function to a text file. Is it possible to do this within the PostgreSQL PL/pgSQL language? I have done this before in Oracle using the UTL_FILE commands and I was hoping that PostgreSQL had similar functionality. Thanks in advance for any help that you can give.

Kindly provide some examples of file operations.

        CREATE OR REPLACE FUNCTION NP_AC015_FETCH.proc_log (P_MSG text, P_MODE integer default 1) RETURNS VOID AS $body$
    DECLARE

        V_F_IS_OPEN boolean;      --IF LOG FILE IS ALREADY OPEN THIS IS SET TO TRUE
        V_LOG_MSG varchar(32767); --LOG FILE NAME
        V_LOG_DIR varchar(30) := 'ND_GANJIS_LOG_DIR'; --LOG DIRECOTY

        vTemp UTL_FILE.FILE_TYPE;
    BEGIN
        select get_var('GM_LOG_FILE') INTO vTemp;

        V_F_IS_OPEN := utl_file.is_open(vTemp);

        if not V_F_IS_OPEN then

          -- Log File Open
          -- 32767 IS THE MAXIMUM NUMBER OF CHARACTERS PER LINE, INCLUDING THE NEWLINE CHARACTER, FOR THIS FILE.
          vTemp := UTL_FILE.FOPEN(V_LOG_DIR, 'NIA_PLSQL_'||to_char(clock_timestamp(), 'yyyymmdd')||'.log', 'A', 32767);

        end if;

        -- LOG MSG TO BE WRITTEN TO THE LOG FILE
        V_LOG_MSG := TO_CHAR(CURRENT_TIMESTAMP, 'yyyy/mm/dd hh24:mi:ss:ff3') ||' '|| P_MSG;

        --Output messages to a file
        UTL_FILE.PUT_LINE(vTemp, V_LOG_MSG);

    --Closing Log File
    if P_MODE = current_setting('NP_AC015_FETCH.PV_LOG_CLOSE_MODE')::pls_integer and utl_file.is_open(vTemp) then

          utl_file.fclose(vTemp);

    end if;

    --HERE THE EXCEPTION PART IS NOT INCLUDED,
    --Reason: PROGRAM WILL GO ON INFINITE LOOP IF SOME ERROR OCCURS HERE, BECAUSE, EACH EXCEPTION WRITES INTO
    --LOG FILE, USING THIS PROCEDURE.
   exception

    when others then

      RAISE EXCEPTION '%', dbms_utility.format_error_backtrace||chr(10)||dbms_utility.format_error_stack||chr(10)||dbms_utility.format_call_stack, true;

    END;
$body$
LANGUAGE PLPGSQL;
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can install the adminpack contrib module and use the function pg_file_write(filename text, data text, append boolean).

Note that this function is restricted to superusers, but you can create a SECURITY DEFINER function owned by a superuser that provides the necessary functionality to the users you choose.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda