2017-03-14 2 views
0

나는 작은 앱을 쓰고있어. 그리고 이제 앱 콘솔에 postgres "notice notice"메시지를 기록해야합니다. PostgreSQL은이go go app에서 postgresql "raise notice"를 보는 방법

예 (내가 lib 디렉토리/PQ 드라이버를 사용하여 이동/SQL 해요) * sql.Conn에 저장된 메시지를 제기 어디를 찾을 수 없습니다

, 내가 전화를 이동 응용 프로그램에서

create function show_mes() returns int as $$ 
begin 
    raise notice 'test message from pg'; 
    return 1; 
end 
$$ language plpgsql; 

이 함수는 결과를 얻을 수 있습니다.

하지만 go 응용 프로그램에서 "pg에서 메시지 테스트"라는 메시지에 어떻게 액세스 할 수 있습니까?

enter image description here

당신을 감사합니다 : 우리가 debbugging에 대한 콘솔에 로그 메시지를 노드에 기록 된 최신 버전에서

!

+2

가능한 중복 골란] (http://stackoverflow.com/questions/42070023/how-do-i-get-postgresql-procedure-warning-messages-in-golang) – Zoyd

답변

1

lib/pq을 사용하는 경우 원하는 것을 얻기 위해 LISTEN/NOTIFY 지원을 사용할 수 있습니다. 그런 다음 포스트 그레스 기능은 다음과 같을 것이다

package main 

import (
    "log" 
    "time" 

    "github.com/lib/pq" 
) 

func main() { 
    dburl := "postgres:///?sslmode=disable" 
    li := pq.NewListener(dburl, 10*time.Second, time.Minute, func(event pq.ListenerEventType, err error) { 
     if err != nil { 
      log.Fatal(err) 
     } 
    }) 

    if err := li.Listen("raise_notice"); err != nil { 
     panic(err) 
    } 

    for { 
     select { 
     case n := <-li.Notify: 
      // n.Extra contains the payload from the notification 
      log.Println("notification:", n.Extra) 
     case <-time.After(5 * time.Minute): 
      if err := li.Ping(); err != nil { 
       panic(err) 
      } 
     } 
    } 
} 

을 : :이 같은 것을 사용할 수있는 이동 측면에서

CREATE FUNCTION show_mes() RETURNS int AS $$ 
BEGIN 
    PERFORM pg_notify('raise_notice', 'test message from pg'); 
    RETURN 1; 
END 
$$ LANGUAGE plpgsql; 
내가에서 PostgreSQL을 절차 경고 메시지를 얻는 방법 [의