记录 SQL 脚本错误
我有一个带有以下命令的 postgres 文件的 SQL 脚本。
COPY product_master(productId, productName) FROM 'product.txt' DELIMITERS ',' CSV;
我想处理此命令的错误(记录错误)
示例
错误:违反重复键值。
COPY
命令是否返回任何值?如果不是,那么如何记录 shell 脚本的输出?
I have a SQL script for postgres file with following command.
COPY product_master(productId, productName) FROM 'product.txt' DELIMITERS ',' CSV;
I want to handle errors of this command (log the error)
example
ERROR: duplicate key value violates.
Does the COPY
command return any value ? If no, then how to log the output of the shell script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在数据库日志文件中记录任何和所有消息(错误、警告等)以及大量附加信息。这是标准行为。当然,您的数据库集群必须进行配置才能执行此操作。 在此处阅读详细手册。
根据您的客户端,您还应该能够从数据库服务器获取错误消息作为直接答案。请注意,错误是在与数据输出不同的流上报告的。就像 shell 中的
stout
和stderr
一样。在 shell 中,您可能会调用
psql -f
执行脚本。看看这个演示中发生了什么:在 shell 中创建一个虚拟 SQL 脚本:
将类似的内容放入其中:
执行它:
输出取决于各种 像
client_min_messages
这样的设置:因为我已经配置了
log_statement = all
(除其他外)我的服务器日志内容如下:我不会在高效服务器上使用
log_statement = all
。这会产生巨大日志文件。You can log any and all messages (error, warning, ..) with a plethora of additional information in the database log file. This is standard behavior. Of course your database cluster has to be configured to do so. Read the fine manual here.
Dpending on your client you should also be able to get error messages as direct answer from the database server. Note that errors are reported on a different stream than data output. Like
stout
andstderr
in the shell.From the shell you would probably call
psql -f
to execute a script. See what happens in this demo:Create a dummy SQL script in the shell:
Put something like this into it:
Execute it:
Output depends on various settings like
client_min_messages
:Since I have configured
log_statement = all
(among others) my server log reads:I would not use
log_statement = all
on a productive server. That produces huge log files.