如何使postgresql只记录日志中的DDL语句?

时间:2018-05-15 09:51:05

标签: postgresql

我想在postgresql日志中使用 DDL语句。

我已将log_statement设置为ddl,并将log_min_messages更改为' log'但我仍然得到这样的日志:

< 2018-05-15 05:10:25.078 EDT > LOG:  MultiXact member wraparound protections are now enabled  
< 2018-05-15 05:10:25.079 EDT > LOG:  database system is ready to accept connections  
< 2018-05-15 05:10:25.085 EDT > LOG:  autovacuum launcher started

我只想要DDL语句,因为我想从日志生成DDL补丁以在生产环境中同步数据库。 还有更好的办法吗?

1 个答案:

答案 0 :(得分:1)

调查https://github.com/pgaudit/pgaudit 或者如果您要对其进行编码,请考虑使用https://www.postgresql.org/docs/current/static/event-triggers.html

例如:

so=# create or replace function notice_ddl() returns event_trigger as $$
begin
raise info '%', session_user || ' ran '||tg_tag||' '||current_query();
end;
$$ language plpgsql;
CREATE FUNCTION
so=# create or replace function notice_ddl() returns event_trigger as $$
begin
raise info '%', session_user || ':: ran "'||tg_tag||'" ('||current_query()||')';
end;
$$ language plpgsql;
CREATE FUNCTION
so=# create event trigger etg on ddl_command_start execute procedure notice_ddl();
CREATE EVENT TRIGGER
so=# create table so(i int);
INFO:  vao:: ran "CREATE TABLE" (create table so(i int);)
CREATE TABLE

当然你可以将声明保存到通知频道表而不是rasing info ...

如果你想使用postgres日志,look into csv logs