psql中,为什么有些命令没有效果?

发布于 2025-01-11 22:03:56 字数 979 浏览 0 评论 0原文

有时我在 psql 中的命令似乎没有效果。知道为什么吗?

下面是数据库library_development中所有表的列表:

library_development=> \d

               List of relations
 Schema |       Name        | Type  |  Owner
--------+-------------------+-------+----------
 public | Pavan             | table | postgres
 public | schema_migrations | table | sai
(2 rows)

在此之后,我使用删除了表Pavan

library_development-> drop table Pavan

但是该表不是没有删除,其显示如下

library_development=> \d
               List of relations
 Schema |       Name        | Type  |  Owner
--------+-------------------+-------+----------
 public | Pavan             | table | postgres
 public | schema_migrations | table | sai
(2 rows)

另外:

  1. 我在 Windows 中使用 PostgreSQL。是否有任何命令可以清除控制台(例如 Oracle 中的 cl scr)?

  2. 在使用 DML 脚本时,我需要在 Postgresql 中执行“提交”的概念吗?

Sometimes my commands in psql seem to be having no effect. Any idea why?

The below is the list of all tables in the database library_development:

library_development=> \d

               List of relations
 Schema |       Name        | Type  |  Owner
--------+-------------------+-------+----------
 public | Pavan             | table | postgres
 public | schema_migrations | table | sai
(2 rows)

After this I dropped the table Pavan using:

library_development-> drop table Pavan

But the Table isn't dropped and its shows as shown:

library_development=> \d
               List of relations
 Schema |       Name        | Type  |  Owner
--------+-------------------+-------+----------
 public | Pavan             | table | postgres
 public | schema_migrations | table | sai
(2 rows)

Also:

  1. I am using PostgreSQL in Windows. Is there any command to clear the console (Like cl scr present in Oracle)?

  2. Is there any concept of a "commit" I need to perform in Postgresql when working with DML scripts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反差帅 2025-01-18 22:03:56

语句以分号结束。

在 psql 中,按下不带分号的 Enter 键会将语句继续到下一行,将写入的内容添加到查询缓冲区而不是执行它。您会注意到提示符从 dbname=> 更改为 dbname-> 以指示您位于续行上。

regress=> DROP TABLE sometable
regress-> \r
Query buffer reset (cleared).
regress=> DROP TABLE sometable;
ERROR:  table "sometable" does not exist
regress=> 

请注意,在我按不带分号的 Enter 后,提示符更改为 regress-# 并且不执行任何操作。没有表sometable,因此如果语句运行就会报告错误。

接下来看下一行\r的使用?这会清除查询缓冲区。请注意,当缓冲区被清除时,提示会变回 regress=#,因为不再缓冲任何部分语句。

这显示了语句如何跨行分割:

regress=> DROP TABLE
regress-> sometable
regress-> ;
ERROR:  table "sometable" does not exist

令人困惑的是,psql 反斜杠命令如 \d 是换行符终止的,而不是分号终止的,所以它们 当你按下回车键时运行。当您想在编写语句时查看表定义时,这很方便,但对于新手来说有点令人困惑。

至于您的其他问题:

  1. 如果 Windows 的 psql 中有“清除屏幕”命令,我还没有找到它。在 Linux 上,我只使用 control-L,与任何其他使用 readline 的程序相同。在 Windows \! cls 可以工作。

  2. PostgreSQL 中的 DDL 是事务性的。您可以BEGIN事务,发出一些DDL,然后COMMIT事务以使其生效。如果您不在显式事务中执行 DDL,那么它会立即生效。

Statements end with semicolons.

In psql, pressing enter without a semicolon continues the statement onto the next line, adding what you wrote to the query buffer rather than executing it. You will notice that the prompt changes from dbname=> to dbname-> to indicate that you're on a continuation line.

regress=> DROP TABLE sometable
regress-> \r
Query buffer reset (cleared).
regress=> DROP TABLE sometable;
ERROR:  table "sometable" does not exist
regress=> 

Notice how after I press enter without a semicolon, the prompt changes to regress-# and no action is taken. There is no table sometable, so if the statement had run an error would be reported.

Next, see the use of \r on the next line? That clears the query buffer. Notice that the prompt changes back to regress=# when the buffer is cleared, as there's no partial statement buffered anymore.

This shows how statements can be split across lines:

regress=> DROP TABLE
regress-> sometable
regress-> ;
ERROR:  table "sometable" does not exist

The confusing thing is that psql backslash commands like \d are newline-terminated, not semicolon terminated, so they do run when you press enter. That's handy when you want to (say) view a table definition while writing a statement, but it's a bit confusing for newcomers.

As for your additional questions:

  1. If there's a "clear screen" command in psql for Windows I haven't found it yet. On Linux I just use control-L, same as any other readline-using program. In Windows \! cls will work.

  2. DDL in PostgreSQL is transactional. You can BEGIN a transaction, issue some DDL, and COMMIT the transaction to have it take effect. If you don't do your DDL in an explicit transaction then it takes effect immediately.

怕倦 2025-01-18 22:03:56

我刚刚遇到了类似的事情,但原因不同。

我试图使用 dropdb 命令,但我所看到的就像我的命令没有效果(提示符的更改意味着我的命令在缓冲区中,但这不是这里的问题):

postgres=# dropdb databasename
postgres-#

像 dropdb 这样的命令应该在 psql 外部输入。我一直在做的是:

$ psql postgres
postgres=# dropdb databasename

注意第一个命令是在 Bash 中并触发 psql,第二个命令是在 psql 中发送的。

正确的方法是直接在 bash(控制台)中发送命令:

$ dropdb databasename

I just encountered something similar, but the cause was different.

I was trying to use the dropdb command and what I was seeing was like my command having no effect (change of prompt means my command is in the buffer, but that's not the issue here):

postgres=# dropdb databasename
postgres-#

Commands like dropdb should be entered OUTSIDE psql. What i had been doing was:

$ psql postgres
postgres=# dropdb databasename

Notice the first command is in Bash and fires psql, and the second is sent within psql.

The correct way is to send the command directly in bash (the console):

$ dropdb databasename
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文