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

embedded/2024/9/25 15:17:42/

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.ppmy.cn/embedded/29532.html

相关文章

鸿蒙学习1概况

鸿蒙学习1相关概念 前言相关概念Stage 模型1. AbilityStage2. UIAbility组件和ExtensionAbility组3. WindowStage4. Context 事件传递UIAbility组件与UI的数据同步UIAbility组件间交互&#xff08;设备内&#xff09; 进程模型线程模型 前言 有时间多看官网&#xff0c;官网的…

零知识证明与同态加密:隐私计算的双剑

PrimiHub一款由密码学专家团队打造的开源隐私计算平台&#xff0c;专注于分享数据安全、密码学、联邦学习、同态加密等隐私计算领域的技术和内容。 在数字时代&#xff0c;隐私保护已成为全球关注的焦点。隐私计算作为解决数据隐私问题的关键技术&#xff0c;其核心目标是在不泄…

【C++ 问题总结】

C问题总结 C问题解决1.C两种实用方式解决clion不能运行多个main文件1.下载插件1.1下载插件&#xff0c;C/C Single File Execution1.2删除CMakeLists.txt文件中的add_executable()1.3在新建的cpp文件中&#xff0c;右击 --> 点击Add executable for single c/cpp file -->…

附录6-4 黑马优购项目-分类和购物车

目录 1 分类 1.1 接口 1.2 窗口限制 1.3 选中状态样式判断 1.4 点击左侧时右侧会到顶点 1.5 源码 2 购物车 2.1 store 2.2 tabBar徽标 2.3 滑动删除 2.4 结算 2.4.1 结算前登录 2.4.2 结算功能 2.5 触发组件事件 2.6 源码 1 分类 分类最上部是…

什么是oneflow

一&#xff0c;什么是OneFlow&#xff1f; OneFlow是一个用于机器学习的开源软件框架&#xff0c;它允许研究人员和开发人员设计、训练和部署机器学习模型。机器学习是人工智能的一个分支&#xff0c;它使计算机能够从数据中学习并做出预测或决策&#xff0c;而不需要明确编程…

docker各目录含义

目录含义builder构建docker镜像的工具或过程buildkit用于构建和打包容器镜像&#xff0c;官方构建引擎&#xff0c;支持多阶段构建、缓存管理、并行化构建和多平台构建等功能containerd负责容器生命周期管理&#xff0c;能起、停、重启&#xff0c;确保容器运行。负责镜管理&am…

I2C接口18路LED呼吸灯驱动IS31FL3218互相替代SN3218替换HTR3218

I2C接口18路LED呼吸灯控制电路IC 该型号IC为QFN24接口&#xff0c;属于小众产品&#xff0c;IS31FL3218、SN3218、HTR3218S管脚兼容&#xff0c;需要注意的是HTR3218管脚与其他型号不兼容。 I2C接口可实现多个LED灯的呼吸灯控制&#xff0c;可实现单色控制18个LED灯&#xff0…

AJAX家政系统 自营+多商家(高级授权)+独立端口 -源码下载

应用介绍 后台&#xff1a;https://service.hnajax.com/hxeJVakAdf.php/index/login AJAX家政系统 自营多商家(高级授权)独立端口 基于FastAdmin和原生微信小程序开发的一款同城预约、上门服务、到店核销家政系统&#xff0c;用户端、服务端(高级授权)、门店端(高级授权)各端…