psql中,为什么有些命令没有效果?
有时我在 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)
另外:
我在 Windows 中使用 PostgreSQL。是否有任何命令可以清除控制台(例如 Oracle 中的 cl scr)?
在使用 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:
I am using PostgreSQL in Windows. Is there any command to clear the console (Like cl scr present in Oracle)?
Is there any concept of a "commit" I need to perform in Postgresql when working with DML scripts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
语句以分号结束。
在 psql 中,按下不带分号的 Enter 键会将语句继续到下一行,将写入的内容添加到查询缓冲区而不是执行它。您会注意到提示符从
dbname=>
更改为dbname->
以指示您位于续行上。请注意,在我按不带分号的 Enter 后,提示符更改为
regress-#
并且不执行任何操作。没有表sometable
,因此如果语句运行就会报告错误。接下来看下一行
\r
的使用?这会清除查询缓冲区。请注意,当缓冲区被清除时,提示会变回regress=#
,因为不再缓冲任何部分语句。这显示了语句如何跨行分割:
令人困惑的是,
psql
反斜杠命令如\d
是换行符终止的,而不是分号终止的,所以它们 当你按下回车键时运行。当您想在编写语句时查看表定义时,这很方便,但对于新手来说有点令人困惑。至于您的其他问题:
如果 Windows 的
psql
中有“清除屏幕”命令,我还没有找到它。在 Linux 上,我只使用 control-L,与任何其他使用 readline 的程序相同。在 Windows\! cls
可以工作。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 fromdbname=>
todbname->
to indicate that you're on a continuation line.Notice how after I press enter without a semicolon, the prompt changes to
regress-#
and no action is taken. There is no tablesometable
, 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 toregress=#
when the buffer is cleared, as there's no partial statement buffered anymore.This shows how statements can be split across lines:
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:
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.DDL in PostgreSQL is transactional. You can
BEGIN
a transaction, issue some DDL, andCOMMIT
the transaction to have it take effect. If you don't do your DDL in an explicit transaction then it takes effect immediately.我刚刚遇到了类似的事情,但原因不同。
我试图使用 dropdb 命令,但我所看到的就像我的命令没有效果(提示符的更改意味着我的命令在缓冲区中,但这不是这里的问题):
像 dropdb 这样的命令应该在 psql 外部输入。我一直在做的是:
注意第一个命令是在 Bash 中并触发 psql,第二个命令是在 psql 中发送的。
正确的方法是直接在 bash(控制台)中发送命令:
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):
Commands like dropdb should be entered OUTSIDE psql. What i had been doing was:
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):