当前位置: 首页 > news >正文

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

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 exitConnection 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 databaseBy 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=# \lList of databasesName    |  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/postgrestemplate1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +|          |          |                 |             |             |            |           | postgres=CTc/postgreswhite     | 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=# \lList of databasesName    |  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/postgrestemplate1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |            |           | =c/postgres          +|          |          |                 |             |             |            |           | postgres=CTc/postgreswhite     | 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 中一个非常实用的工具,对于快速启动一个新项目或进行开发测试来说,可以节省不少时间和精力。

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

http://www.lryc.cn/news/341278.html

相关文章:

  • 软件设计师-重点的构造型设计模式
  • Java面试问题及答案
  • STM32 01
  • 学习笔记:【QC】Android Q - phone 模块
  • webscoket+webrtc实现语音通话
  • PHP源码_众筹商城
  • 智能小程序 Ray 开发——表单组件 Button 和 Checkbox 实操讲解
  • 渗透之sql注入联合查询的注入
  • NLP transformers - 文本分类
  • QT 开发COM(ActiveX)组件基础介绍和方案验证
  • [1673]jsp在线考试管理系统Myeclipse开发mysql数据库web结构java编程计算机网页项目
  • 每日一算法
  • Spring Cloud Gateway直接管理Vue.js的静态资源
  • 14.集合、常见的数据结构
  • NLP从入门到实战——命名实体识别
  • 接口测试工具-postman介绍
  • 日志中看到来自User Agent go-http-client / 1.1的大量请求(go-http-client 1.1)服务器爆了
  • yolov8 区域声光报警+计数
  • 《QT实用小工具·五十五》带有标签、下划线的Material Design风格输入框
  • 用Go实现一个无界资源池
  • Apache Seata基于改良版雪花算法的分布式UUID生成器分析2
  • 13、揭秘JVM垃圾回收器:面试必备知识,你掌握了吗?
  • 治疗耳鸣患者案例分享第二期
  • 数据加密的方法
  • Android BINDER是干嘛的?
  • 运维各种中间件的手动安装(非常详细)
  • 【Android】Android应用性能优化总结
  • FBA头程海运发货流程是怎样的?
  • 二、VLAN原理和配置
  • stackqueue类——适配器模式 双端队列deque(C++)