createdb 是一个用于创建数据库的核心实用程序,它本质上是 CREATE DATABASE SQL 命令的一个包装器。
createdb 在 PostgreSQL 7.0 中被添加。
用法
截至 PostgreSQL 15,createdb 不接受 -d 选项来提供 conninfo 字符串;连接参数必须单独提供或通过环境变量提供。
如果默认的 postgres 数据库不存在,请使用 --maintenance-db 指定一个用于连接的数据库名称。
自 PostgreSQL 8.3 起,createdb 在成功创建数据库时不再发出任何输出。
源代码
createdb 的源代码位于 src/bin/scripts/createdb.c。
变更历史
- PostgreSQL 17
- 选项
--locale-provider接受附加值builtin(提交 2d819a08)
- 选项
- PostgreSQL 16
- 添加了选项
--icu-rules(提交 30a53b79)
- 添加了选项
- PostgreSQL 15
- PostgreSQL 9.2
- 添加了选项
--maintenance-db(提交 68281e00)
- 添加了选项
- PostgreSQL 8.4
- 添加了选项
-w/--no-password(提交 9de59fd1)
- 添加了选项
- PostgreSQL 8.3
- 删除了选项
--quiet(提交 9539e64b)
- 删除了选项
- PostgreSQL 7.4
- 从 shell 脚本转换为 C 程序(提交 9e0ab712)
- PostgreSQL 7.3
- 添加了对包含空格的标识符的支持(提交 5804a7ce)
- PostgreSQL 7.1
- 添加了选项
--template(提交 c3b00e7e)
- 添加了选项
- PostgreSQL 7.0
- 添加(提交 240e4c98)
示例
createdb 的基本用法示例
$ createdb -h localhost -p 5432 -U postgres testdb $
从 template0 创建数据库
$ createdb -h localhost -p 5432 -U postgres --template=template0 testdb
尝试创建一个已存在的数据库
$ createdb -h localhost -p 5432 -U postgres postgres createdb: error: database creation failed: ERROR: database "postgres" already exists
参考资料
- PostgreSQL 文档: createdb
