PostgreSQL自带的命令行工具02- createdb

发布于:2024-05-06 ⋅ 阅读:(29) ⋅ 点赞:(0)

PostgreSQL自带的命令行工具02- createdb

基础信息
OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
DB版本:16.2
pg软件目录:/home/pg16/soft
pg数据目录:/home/pg16/data
端口:5777

createdb 是 PostgreSQL 中的一个命令行工具,用于创建一个新的 PostgreSQL 数据库。该工具实际上是在后台使用 PostgreSQL 的 CREATE DATABASE SQL 命令来创建数据库的。使用 createdb 工具可以让数据库管理员在命令行环境中快速地创建数据库,无需直接进入 SQL 命令行接口。

通过help查看帮助文档。

[pg16@test ~]$ createdb --help
createdb creates a PostgreSQL database.

Usage:
  createdb [OPTION]... [DBNAME] [DESCRIPTION]

Options:
  -D, --tablespace=TABLESPACE  default tablespace for the database
  -e, --echo                   show the commands being sent to the server
  -E, --encoding=ENCODING      encoding for the database
  -l, --locale=LOCALE          locale settings for the database
      --lc-collate=LOCALE      LC_COLLATE setting for the database
      --lc-ctype=LOCALE        LC_CTYPE setting for the database
      --icu-locale=LOCALE      ICU locale setting for the database
      --icu-rules=RULES        ICU rules setting for the database
      --locale-provider={libc|icu}
                               locale provider for the database's default collation
  -O, --owner=OWNER            database user to own the new database
  -S, --strategy=STRATEGY      database creation strategy wal_log or file_copy
  -T, --template=TEMPLATE      template database to copy
  -V, --version                output version information, then exit
  -?, --help                   show this help, then exit

Connection options:
  -h, --host=HOSTNAME          database server host or socket directory
  -p, --port=PORT              database server port
  -U, --username=USERNAME      user name to connect as
  -w, --no-password            never prompt for password
  -W, --password               force password prompt
  --maintenance-db=DBNAME      alternate maintenance database

By default, a database with the same name as the current user is created.

Report bugs to <pgsql-bugs@lists.postgresql.org>.
PostgreSQL home page: <https://www.postgresql.org/>

示例1

创建一个名为 “white1” 的数据库:

--创建数据库
[pg16@test ~]$ createdb white1

[pg16@test ~]$ psql -p 5777
psql (16.2)
Type "help" for help.

postgres=# \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 white     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 white1    | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
(5 rows)

这会使用当前用户的默认设置来创建一个新数据库。如果你需要以不同的用户身份来创建数据库,你可以使用 -U 选项来指定用户名,例如:

createdb -U username mydatabase

在这里,username 是 PostgreSQL 中具有创建数据库权限的用户。

示例2

创建数据库white2,指定用户为test2

[pg16@test ~]$ createdb -U test2 white2
[pg16@test ~]$ 
[pg16@test ~]$ psql -p 5777
psql (16.2)
Type "help" for help.

postgres=# \l
                                                       List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | ICU Locale | ICU Rules |   Access privileges   
-----------+----------+----------+-----------------+-------------+-------------+------------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +
           |          |          |                 |             |             |            |           | postgres=CTc/postgres
 white     | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 white1    | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
 white2    | test2    | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | 
(6 rows)

主要选项

  • -U <username>:指定运行 createdb 命令的 PostgreSQL 用户。
  • -h <hostname>:指定 PostgreSQL 服务器的主机名,如果你的 PostgreSQL 服务器不是运行在本地机器上。
  • -p <port>:指定 PostgreSQL 服务器的端口,如果不是使用默认端口(默认是 5432)。
  • -e:显示命令执行的 SQL 语句。
  • -T <template>:指定一个模板数据库。新数据库将从指定的模板数据库克隆而来。
  • -O <owner>:指定新数据库的拥有者。

注意

  • 使用 createdb 之前,确保 PostgreSQL 服务正在运行,并且你具有足够的权限来创建数据库。
  • 如果数据库创建成功,createdb 命令通常不会有输出。如果有错误发生(比如数据库已经存在),它会显示错误信息。
  • 一些复杂的数据库创建选项(如编码、表空间等)可能需要直接使用 SQL CREATE DATABASE 命令来指定。

createdb 是 PostgreSQL 中一个非常实用的工具,对于快速启动一个新项目或进行开发测试来说,可以节省不少时间和精力。

谨记:心存敬畏,行有所止。