Postgresql 中文操作指南
1.4. Accessing a Database #
创建数据库后,可以通过以下方式访问它:
Once you have created a database, you can access it by:
你可能希望启动 psql 来尝试本练习中的示例。可以通过键入以下命令来对 mydb 数据库激活它:
You probably want to start up psql to try the examples in this tutorial. It can be activated for the mydb database by typing the command:
$ psql mydb
如果您未提供数据库名,则它将默认为您的用户帐户名。您已在上一节中使用 createdb 发现了此方案。
If you do not supply the database name then it will default to your user account name. You already discovered this scheme in the previous section using createdb.
在 psql 中,您会看到以下消息:
In psql, you will be greeted with the following message:
psql (16.3)
Type "help" for help.
mydb=>
最后一行也可以是:
The last line could also be:
mydb=#
这意味着您是数据库超级用户,如果您自己安装了 PostgreSQL 实例,这很可能是这种情况。作为超级用户意味着您不受访问控制的约束。对于本教程而言,这并不重要。
That would mean you are a database superuser, which is most likely the case if you installed the PostgreSQL instance yourself. Being a superuser means that you are not subject to access controls. For the purposes of this tutorial that is not important.
如果您在启动 psql 时遇到问题,请返回上一节。createdb 和 psql 的诊断是相似的,如果前者有效,后者也应该有效。
If you encounter problems starting psql then go back to the previous section. The diagnostics of createdb and psql are similar, and if the former worked the latter should work as well.
psql 打印的最后一行是提示,它表明 psql 正在监听您,并且您可以将 SQL 查询输入到 psql 维护的工作空间。尝试以下命令:
The last line printed out by psql is the prompt, and it indicates that psql is listening to you and that you can type SQL queries into a work space maintained by psql. Try out these commands:
mydb=> SELECT version();
version
------------------------------------------------------------------------------------------
PostgreSQL 16.3 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
(1 row)
mydb=> SELECT current_date;
date
------------
2016-01-07
(1 row)
mydb=> SELECT 2 + 2;
?column?
----------
4
(1 row)
psql 程序具有许多不是 SQL 命令的内部命令。它们以反斜杠字符“\”开头。例如,可以通过输入以下内容来获取有关各种 PostgreSQL SQL 命令的语法帮助:
The psql program has a number of internal commands that are not SQL commands. They begin with the backslash character, “\”. For example, you can get help on the syntax of various PostgreSQL SQL commands by typing:
mydb=> \h
要退出 psql,请键入:
To get out of psql, type:
mydb=> \q
并且 psql 将退出并返回到您的命令 shell。(对于更多内部命令,请在 psql 提示符处键入 \? 。) psql 的全部功能在 psql 中有记录。在本教程中,我们不会明确使用这些特性,但在需要时,您可以自己使用它们。
and psql will quit and return you to your command shell. (For more internal commands, type \? at the psql prompt.) The full capabilities of psql are documented in psql. In this tutorial we will not use these features explicitly, but you can use them yourself when it is helpful.