如何在数据库外使用斜杠命令?
我尝试在数据库之外使用查询。即无需登录数据库 我想得到结果。我找到了选项(-c)。使用该选项,我们可以从数据库外部执行查询:
test:~$ psql -U sat -c "select * from test.details";
它给出输出。我想将该查询用于 crontab 条目。所以我尝试将输出存储在文件中:
test:~$ psql -U sat -c "select * from test.details \g sat";
产生错误:
错误:“\”处或附近的语法错误 第 1 行:从 test.details \g sat 中选择 *
怎么做呢?
I tried to use the query outside of the database. That is, without login to data base
I want to get the result. I found the option (-c). Using that option we can execute the query from outside the data base:
test:~$ psql -U sat -c "select * from test.details";
It gives the output. I want to use that query for a crontab entry. So I have tried to store the output in a file:
test:~$ psql -U sat -c "select * from test.details \g sat";
Produced an error:
ERROR: syntax error at or near "\" LINE 1: select * from test.details \g sat
How to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是斜杠,而是反斜杠。
反斜杠是 PostgreSQL 字符串文字中的转义字符,因此您必须将其加倍才能将单个反斜杠放入实际数据中。
如果您想从命令行将查询结果存储到文件中,则必须使用
-o
命令行选项,因此您的查询将变为:This is not a slash, but a backslash .
Backslash is an escape character in PostgreSQL string literals, therefore you have to double it to get a single backslash into the actual data.
If you want to store the result of a query into a file from the command line you have to use the
-o
command line option,so your query will become :不存在“数据库外部查询”或“无需登录数据库”这样的事情。
您正在尝试将 psql 客户端的元命令与 SQL 命令混合,这绝对是不可能的。反斜杠元命令由 psql 客户端解释,SQL 查询由数据库服务器解释。
psql 中的大多数元命令实际上都转换为对数据库服务器的(一系列)SQL 查询。如果您在交互模式下使用命令选项
-E
启动 psql,则可以使 psql 打印它发送到数据库引擎的命令。尝试:然后执行任何反斜杠命令并观察输出。对于你的其余问题,@aleroot 已经给出了很好的建议。
There is no such thing as a "query outside of the data base" or "without login to data base".
You are trying to mix meta-commands of the psql client with SQL commands, which is strictly impossible. The backslash meta commands are interpreted by the psql client, SQL queries are interpreted by the database server.
Most meta-commands in psql are actually translated into (a series of) SQL queries to the database server. You can make psql print the commands it sends to the database engine if you start it up with the command option
-E
in interactive mode. Try:And then execute any backslash command and observe the output. For the rest of your question @aleroot has already given good advice.