Postgresql 简明教程
PostgreSQL - CREATE Database
本章讨论如何在 PostgreSQL 中创建新数据库。PostgreSQL 提供两种创建新数据库的方法:
This chapter discusses about how to create a new database in your PostgreSQL. PostgreSQL provides two ways of creating a new database −
-
Using CREATE DATABASE, an SQL command.
-
Using createdb a command-line executable.
Using CREATE DATABASE
此命令将从 PostgreSQL shell 提示符创建一个数据库,但您必须具有创建数据库的适当权限。默认情况下,新数据库将通过克隆标准系统数据库模板 1 来创建。
This command will create a database from PostgreSQL shell prompt, but you should have appropriate privilege to create a database. By default, the new database will be created by cloning the standard system database template1.
Using createdb Command
PostgreSQL 命令行可执行文件 createdb 是对 SQL 命令 CREATE DATABASE 的包装。此命令和 SQL 命令 CREATE DATABASE 之间的唯一区别在于前者可以直接从命令行运行,并且允许在一个命令中向数据库中添加注释。
PostgreSQL command line executable createdb is a wrapper around the SQL command CREATE DATABASE. The only difference between this command and SQL command CREATE DATABASE is that the former can be directly run from the command line and it allows a comment to be added into the database, all in one command.
Syntax
createdb 的语法如下所示:
The syntax for createdb is as shown below −
createdb [option...] [dbname [description]]
Parameters
下表列出了参数及其说明。
The table given below lists the parameters with their descriptions.
S. No. |
Parameter & Description |
1 |
dbname The name of a database to create. |
2 |
description Specifies a comment to be associated with the newly created database. |
3 |
options command-line arguments, which createdb accepts. |
Options
下表列出了 createdb 可接受的命令行参数:
The following table lists the command line arguments createdb accepts −
S. No. |
Option & Description |
1 |
-D tablespace Specifies the default tablespace for the database. |
2 |
-e Echo the commands that createdb generates and sends to the server. |
3 |
-E encoding Specifies the character encoding scheme to be used in this database. |
4 |
-l locale Specifies the locale to be used in this database. |
5 |
-T template Specifies the template database from which to build this database. |
6 |
--help Show help about createdb command line arguments, and exit. |
7 |
-h host Specifies the host name of the machine on which the server is running. |
8 |
-p port Specifies the TCP port or the local Unix domain socket file extension on which the server is listening for connections. |
9 |
-U username User name to connect as. |
10 |
-w Never issue a password prompt. |
11 |
-W Force createdb to prompt for a password before connecting to a database. |
打开命令提示符并转到PostgreSQL安装所在的目录。转到bin目录并执行以下命令来创建数据库。
Open the command prompt and go to the directory where PostgreSQL is installed. Go to the bin directory and execute the following command to create a database.
createdb -h localhost -p 5432 -U postgres testdb
password ******
给定的上面命令将提示您提供PostgreSQL管理员用户的密码,默认情况下,密码为 postgres 。因此,请提供一个密码并继续创建新数据库
The above given command will prompt you for password of the PostgreSQL admin user, which is postgres, by default. Hence, provide a password and proceed to create your new database
使用上述方法之一创建数据库后,可以使用 \l (即反斜杠el命令)按如下方式检查数据库列表:-
Once a database is created using either of the above-mentioned methods, you can check it in the list of databases using \l, i.e., backslash el command as follows −
postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
testdb | postgres | UTF8 | C | C |
(4 rows)
postgres-#