【问题标题】:Stop PostgreSQL Printing Errors to STDOUT停止 PostgreSQL 打印错误到 STDOUT
【发布时间】:2017-07-19 18:29:18
【问题描述】:

当我运行有错误的查询时,我在标准输出上收到错误消息。我认为这些是由 PostgrSQL 打印出来的,但我不确定。以下是我可以用来重现此问题的步骤:

(vpgrstest) Euphorbus:~/tmp$ pg_ctl -D /usr/local/var/postgres start
server starting
LOG:  database system was shut down at 2017-07-19 17:56:28 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started
Euphorbus:~/tmp$ pyenv virtualenv 3.6.2 vpgrstest
Requirement already satisfied: setuptools in /Users/rgant/.pyenv/versions/3.6.2/envs/vpgrstest/lib/python3.6/site-packages
Requirement already satisfied: pip in /Users/rgant/.pyenv/versions/3.6.2/envs/vpgrstest/lib/python3.6/site-packages
(vpgrstest) Euphorbus:~/tmp$ pip install psycopg2
Collecting psycopg2
  Using cached psycopg2-2.7.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Installing collected packages: psycopg2
Successfully installed psycopg2-2.7.1
(vpgrstest) Euphorbus:~/tmp$ python test.py
ERROR:  null value in column "num" violates not-null constraint
DETAIL:  Failing row contains (1, null).
STATEMENT:  INSERT INTO test (num) VALUES (NULL)
Traceback (most recent call last):
  File "test.py", line 5, in 
    cur.execute("INSERT INTO test (num) VALUES (%s)", (None, ))
psycopg2.IntegrityError: null value in column "num" violates not-null constraint
DETAIL:  Failing row contains (1, null).

(vpgrstest) Euphorbus:~/tmp$ python test.py 2>/dev/null
ERROR:  null value in column "num" violates not-null constraint
DETAIL:  Failing row contains (1, null).
STATEMENT:  INSERT INTO test (num) VALUES (NULL)

test.py的内容是:

import psycopg2
conn = psycopg2.connect("postgresql://rgant@localhost/rgant")
cur = conn.cursor()
cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer NOT NULL);")
cur.execute("INSERT INTO test (num) VALUES (%s)", (None, ))

我不希望看到以“ERROR:”、“DETAIL:”或“STATEMENT:”开头的前三行打印到标准输出。

知道是什么原因造成的吗?我使用自制软件安装了 postgresql。

(vpgrstest) Euphorbus:~/tmp$ brew info postgresql
postgresql: stable 9.6.3 (bottled), HEAD
Object-relational database system
https://www.postgresql.org/
Conflicts with:
  postgres-xc (because postgresql and postgres-xc install the same binaries.)
/usr/local/Cellar/postgresql/9.6.3 (3,259 files, 36.6MB) *
  Poured from bottle on 2017-07-05 at 14:51:21
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/postgresql.rb
==> Dependencies
Required: openssl ✔, readline ✔
==> Requirements
Optional: python ✘, python3 ✔
==> Options
--with-dtrace
    Build with DTrace support
--with-python
    Enable PL/Python2
--with-python3
    Enable PL/Python3 (incompatible with --with-python)
--without-perl
    Build without Perl support
--without-tcl
    Build without Tcl support
--HEAD
    Install HEAD version
==> Caveats
If builds of PostgreSQL 9 are failing and you have version 8.x installed,
you may need to remove the previous version first. See:
  https://github.com/Homebrew/legacy-homebrew/issues/2510

To migrate existing data from a previous major version (pre-9.0) of PostgreSQL, see:
  https://www.postgresql.org/docs/9.6/static/upgrading.html

To migrate existing data from a previous minor version (9.0-9.5) of PostgreSQL, see:
  https://www.postgresql.org/docs/9.6/static/pgupgrade.html

  You will need your previous PostgreSQL installation from brew to perform `pg_upgrade`.
  Do not run `brew cleanup postgresql` until you have performed the migration.

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start

需要明确的是,我的问题不在于存在错误(我故意制造错误),而只是错误正在由 PostgreSQL(或者可能是 psycopg2)打印出来

【问题讨论】:

    标签: python postgresql python-3.x homebrew psycopg2


    【解决方案1】:

    这种行为的原因是您启动 pgsql 的方式,因为您从命令行发出命令以启动 postgres,它在后台运行并报告给标准输出。

    更改命令:

    pg_ctl -D /usr/local/var/postgres start
    

    收件人:

    pg_ctl -D /usr/local/var/postgres start > /dev/null
    

    您将不会再看到这些错误消息。或者,如果您确实想看到它们但不希望它们污染您的 python 输出。

    pg_ctl -D /usr/local/var/postgres -l /some/log/path.log start
    

    或者您可以打开 2 个终端窗口并在一个窗口中运行 pg_ctl 命令,在另一个窗口中运行您的 python 脚本。

    【讨论】:

    猜你喜欢
    • 2017-02-19
    • 1970-01-01
    • 2012-02-14
    • 2019-11-30
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多