停止PostgreSQL打印错误到STDOUT

时间:2017-07-19 18:29:18

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

当我运行有错误的查询时,我在stdout上收到错误消息。我认为这些是由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:"或"声明:"打印到stdout。

知道是什么造成的吗?我使用自制软件安装了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)打印出来

1 个答案:

答案 0 :(得分: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脚本。