为什么一些 psql 命令行中的数据库名称后面会跟着 (# 或 '#?

huangapple go评论71阅读模式
英文:

Why do some psql lines have (# or '# after the database name?

问题

我正在Mac上的命令行中使用psql。通常,当我输入一个命令时,开头的行会显示database_name=#。我用一个;来结束命令,这样很方便。

有时,当我输入一个命令(这个命令是从一本我正在学习的教材中复制和粘贴的)时,开头的行会变成以下之一:

postgis_in_action(#

postgis_in_action'#

请问'代表什么意思?如何转义它们以恢复到“正常”状态?

英文:

I'm using psql in the command line on a Mac. Usually, when I type a command, the opening line reads database_name=#. I terminate the command with a ; and that works nicely.

Sometimes when I write a command (which has been copied and pasted from a textbook I'm working through) I end up with the opening line being one of the following:

postgis_in_action(#

postgis_in_action'#

What do the ( and ' mean please? And how do I escape them to get back to 'normal'?

答案1

得分: 1

每当您输入一个多行查询,并且其中包含一些起始和结束括号,例如 ( ),那么开头的行会变化,以提醒用户已经开始了需要关闭的括号。

看下面的例子:

node1=# select
node1-# *
node1-# from
node1-# dept
node1-# where
node1-# deptno=
node1-# 1 and loc in
node1-# (
node1(# 'isb'
node1(# )
node1-# ;
 deptno | dname | loc 
--------+-------+-----
(0 rows)

node1=#

如果用户不小心忘记关闭括号,那么开头的行将显示为 database_name(#

甚至一个只有一行的查询,如果其中有一些括号丢失,也会导致相同的开头行。

> node1=# select * from dept where deptno=1 and loc in ('isb';
> 
> node1(#

要使开头行恢复正常,您必须完成查询或只需按下 <kbd>CTRL</kbd>+<kbd>C</kbd> 键。

<details>
<summary>英文:</summary>

When ever you type a query which is multi line and have some starting and ending brackets e.g ( ) then opening line will change to let user know that he has started a bracket that need to be closed. 

see the below example: 

        node1=# select
    node1-# *
    node1-# from
    node1-# dept
    node1-# where
    node1-# deptno=
    node1-# 1 and loc in
    node1-# (
    node1(# &#39;isb&#39;
    node1(# )
    node1-# ;
     deptno | dname | loc 
    --------+-------+-----
    (0 rows)
    
    node1=#

And if user somehow forget to close the brackets then opening line will show database_name(#

Even a single line query which has some brackets missing result in same opening line. 


&gt; node1=# select * from dept where deptno=1 and loc in (&#39;isb&#39;;
&gt; 
&gt; node1(#

To make the opening line normal you have to complete the query or just press &lt;kbd&gt;CTRL&lt;/kbd&gt;+&lt;kbd&gt;C&lt;/kbd&gt; keys.


</details>



# 答案2
**得分**: 1

你正在输入的命令,在你上次按下"回车"键时,要么处于括号结构的中间,要么处于一个`'`字符串中。它正在等待你关闭它们。它会忽略任何通常用于结束命令的`;`字符,直到你关闭这些结构。

如果你的命令到目前为止是正确的,那么你只需要完成它的编写。

如果你想放弃这个命令,因为你本应该在几行前关闭你的结构,现在关闭它们是错误的,那么你可以按下<kbd>ctrl</kbd>-<kbd>C</kbd>,或者在`(`的情况下,你可以在单独的一行上输入`\r`。在`'`的情况下,你需要在使用`\r`之前关闭它,因为`'\r'`是字符串中的合法内容,所以它不能将你从字符串中转义出来。

此外,你可以通过在单独的一行上输入`\e`来编辑你到目前为止的命令。但同样,在`'`中,你需要先关闭引号,否则`\e`只会成为字符串的一部分。

<details>
<summary>英文:</summary>

The command you are entering was, as of the last time you hit &quot;return&quot;, in the middle of either a parenthesized construct, or a `&#39;` string.  It is waiting for you to close them.  It is going to ignore any normally-command-ending `;` characters it sees until you close out those constructs.  

If your command is correct so far, then you just need to finish writing it.

If you want to abandon this command because you were supposed to close your constructs a few lines ago, and closing them now is the wrong thing to do, then you can hit &lt;kbd&gt;ctrl&lt;/kbd&gt;-&lt;kbd&gt;C&lt;/kbd&gt;, or in the case of `(`, you can type `\r` on a line by itself.  In the case of `&#39;`, you would need to close it before using `\r`, because &#39;\r&#39; is legal content inside a string so it can&#39;t escape you out of the string.

Also, you can edit your command-so-far by typing `\e` on a line by itself.  But again, when in `&#39;` you need to close the quote first, otherwise \e just becomes part of the string.


</details>



# 答案3
**得分**: 1

这表示您缺少闭括号或撇号标记。

正常情况:

postgres=# select pg_size_pretty(pg_relation_size('test'));
pg_size_pretty

8192 bytes
(1 row)


缺少括号/撇号:

postgres=# select pg_size_pretty(pg_relation_size('test')
postgres(# );
pg_size_pretty

8192 bytes
(1 row)

postgres=# select count(*) from test where name = 'vignesh
postgres'# ';
count

10
(1 row)


没有分号的有效查询将有连字符:

postgres=# select count(*) from test where name = 'vignesh'
postgres-# ;
count

100
(1 row)


只需通过&lt;kbd&gt;Ctrl&lt;/kbd&gt; + &lt;kbd&gt;C&lt;/kbd&gt;取消该查询。由于该查询已被取消并且未运行,按上箭头将不会将其显示出来,因为它不会出现在历史记录中。

<details>
<summary>英文:</summary>

It means you missing the closing braces or the apostrophe mark.

Normal one :

    postgres=# select pg_size_pretty(pg_relation_size(&#39;test&#39;));                                                                                                                 
    pg_size_pretty
    ----------------
    8192 bytes
    (1 row)

Missed braces/apostrophe:

    postgres=# select pg_size_pretty(pg_relation_size(&#39;test&#39;)                                                                                                                   
    postgres(# );
    pg_size_pretty
    ----------------
    8192 bytes
    (1 row)

    postgres=# select count(*) from test where name = &#39;vignesh
    postgres&#39;# &#39;;
    count
    -------
    10
    (1 row)

Valid query with no semi-colon will have hyphen:

    postgres=# select count(*) from test where name = &#39;vignesh&#39;
    postgres-# ;
    count
    -------
    100
    (1 row)

Just cancel that query by &lt;kbd&gt;Ctrl&lt;/kbd&gt; + &lt;kbd&gt;C&lt;/kbd&gt;. Since that query is cancelled and not ran, pressing up arrow will not bring that up, as it will not be available in the history.

</details>



huangapple
  • 本文由 发表于 2020年1月3日 18:05:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定