Postgresql 中文操作指南
1.3. Creating a Database #
查看是否可以访问数据库服务器的第一个测试是尝试创建一个数据库。正在运行的 PostgreSQL 服务器可以管理多个数据库。通常,每个项目或每个用户使用一个单独的数据库。
The first test to see whether you can access the database server is to try to create a database. A running PostgreSQL server can manage many databases. Typically, a separate database is used for each project or for each user.
可能是您的网站管理员已经为您创建了一个数据库。在这种情况下,您可以省略此步骤,直接跳到下一部分。
Possibly, your site administrator has already created a database for your use. In that case you can omit this step and skip ahead to the next section.
要创建新数据库,在此示例中名为 mydb,请使用以下命令:
To create a new database, in this example named mydb, you use the following command:
$ createdb mydb
如果这样做没有任何响应,则表示此步骤成功,您可以跳过本节的其余部分。
If this produces no response then this step was successful and you can skip over the remainder of this section.
如果您看到类似以下消息的内容:
If you see a message similar to:
createdb: command not found
则未正确安装 PostgreSQL。它要么根本未安装,要么您的 shell 的搜索路径未设置为包含它。尝试用绝对路径调用命令:
then PostgreSQL was not installed properly. Either it was not installed at all or your shell’s search path was not set to include it. Try calling the command with an absolute path instead:
$ /usr/local/pgsql/bin/createdb mydb
您网站的路径可能不同。请联系您的网站管理员或查看安装说明以纠正这种情况。
The path at your site might be different. Contact your site administrator or check the installation instructions to correct the situation.
另一个响应可能是:
Another response could be this:
createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
这意味着服务器未启动,或者它没有在 createdb 期望联系它的位置侦听。同样,查看安装说明或咨询管理员。
This means that the server was not started, or it is not listening where createdb expects to contact it. Again, check the installation instructions or consult the administrator.
另一个响应可能是:
Another response could be this:
createdb: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "joe" does not exist
在其中提到了你自己的登录名。如果管理员尚未为你创建 PostgreSQL 用户帐户,则会发生这种情况。(PostgreSQL 用户帐户与操作系统用户帐户有所不同。) 如果你是管理员,请参阅 Chapter 22以获得创建帐户的帮助。你将需要成为操作系统用户,PostgreSQL 已安装在其下(通常为 postgres),才能创建第一个用户帐户。也有可能是为你分配了一个与操作系统用户名不同的 PostgreSQL 用户名;如果是这种情况,你需要使用 _-U_转换或设置 _PGUSER_环境变量来指定你的 PostgreSQL 用户名。
where your own login name is mentioned. This will happen if the administrator has not created a PostgreSQL user account for you. (PostgreSQL user accounts are distinct from operating system user accounts.) If you are the administrator, see Chapter 22 for help creating accounts. You will need to become the operating system user under which PostgreSQL was installed (usually postgres) to create the first user account. It could also be that you were assigned a PostgreSQL user name that is different from your operating system user name; in that case you need to use the -U switch or set the PGUSER environment variable to specify your PostgreSQL user name.
如果您有用户帐户,但它没有创建数据库所需的权限,您将看到以下内容:
If you have a user account but it does not have the privileges required to create a database, you will see the following:
createdb: error: database creation failed: ERROR: permission denied to create database
并非每个用户都有权创建新数据库。如果 PostgreSQL 拒绝为您创建数据库,则站点管理员需要授予您创建数据库的权限。如果发生这种情况,请咨询您的站点管理员。如果您自己安装了 PostgreSQL,那么您应该使用启动服务器时的该用户帐户,为此教程的目的登录。[ [1 ]
Not every user has authorization to create new databases. If PostgreSQL refuses to create databases for you then the site administrator needs to grant you permission to create databases. Consult your site administrator if this occurs. If you installed PostgreSQL yourself then you should log in for the purposes of this tutorial under the user account that you started the server as. [1]
您还可以创建具有其他名称的数据库。PostgreSQL 允许您在给定的站点创建任意数量的数据库。数据库名称必须以字母开头的第一个字符,且字节长度不得超过 63。一个方便的选择是创建名称与您当前用户名相同的数据库。许多工具将数据库名称作为默认值,因此可以节省您的一些打字。要创建该数据库,只需输入:
You can also create databases with other names. PostgreSQL allows you to create any number of databases at a given site. Database names must have an alphabetic first character and are limited to 63 bytes in length. A convenient choice is to create a database with the same name as your current user name. Many tools assume that database name as the default, so it can save you some typing. To create that database, simply type:
$ createdb
如果您不再想使用数据库,则可以将其删除。例如,如果您是 mydb 数据库的所有者(创建者),则可以使用以下命令将其销毁:
If you do not want to use your database anymore you can remove it. For example, if you are the owner (creator) of the database mydb, you can destroy it using the following command:
$ dropdb mydb
(对于此命令,数据库名称不会默认为用户帐户名称。您始终需要指定它。)此操作会物理删除与该数据库关联的所有文件并且不能撤消,因此只能在经过深思熟虑之后执行此操作。
(For this command, the database name does not default to the user account name. You always need to specify it.) This action physically removes all files associated with the database and cannot be undone, so this should only be done with a great deal of forethought.
[1 ] 作为此方法起作用的原因的解释:PostgreSQL 用户名与操作系统用户帐户分开。当您连接到数据库时,可以选择连接哪个 PostgreSQL 用户名;如果您不这样做,它将默认为与您当前操作系统帐户相同的名称。事实上,总会有一个 PostgreSQL 用户帐户与启动服务器的操作系统用户同名,并且该用户总是具有创建数据库的权限。您不必登录为该用户,也可以在任何地方指定 -U 选项,以选择要连接的 PostgreSQL 用户名。
[1] As an explanation for why this works: PostgreSQL user names are separate from operating system user accounts. When you connect to a database, you can choose what PostgreSQL user name to connect as; if you don’t, it will default to the same name as your current operating system account. As it happens, there will always be a PostgreSQL user account that has the same name as the operating system user that started the server, and it also happens that that user always has permission to create databases. Instead of logging in as that user you can also specify the -U option everywhere to select a PostgreSQL user name to connect as.