PostgreSQL自带的命令行工具21- initdb

PostgreSQL自带的命令行工具21- initdb

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

initdb 是 PostgreSQL 中的一个工具,用于创建一个新的 PostgreSQL 数据库集群。数据库集群是一组数据库和一些与这些数据库相关的初始化信息,包括配置文件(如 postgresql.confpg_hba.conf)以及事务日志等。实际上,initdb 主要负责初始化数据库系统的文件系统环境。

通过help查看帮助文档。

[pg16@test ~]$ initdb --help
initdb initializes a PostgreSQL database cluster.

Usage:
  initdb [OPTION]... [DATADIR]

Options:
  -A, --auth=METHOD         default authentication method for local connections
      --auth-host=METHOD    default authentication method for local TCP/IP connections
      --auth-local=METHOD   default authentication method for local-socket connections
 [-D, --pgdata=]DATADIR     location for this database cluster
  -E, --encoding=ENCODING   set default encoding for new databases
  -g, --allow-group-access  allow group read/execute on data directory
      --icu-locale=LOCALE   set ICU locale ID for new databases
      --icu-rules=RULES     set additional ICU collation rules for new databases
  -k, --data-checksums      use data page checksums
      --locale=LOCALE       set default locale for new databases
      --lc-collate=, --lc-ctype=, --lc-messages=LOCALE
      --lc-monetary=, --lc-numeric=, --lc-time=LOCALE
                            set default locale in the respective category for
                            new databases (default taken from environment)
      --no-locale           equivalent to --locale=C
      --locale-provider={libc|icu}
                            set default locale provider for new databases
      --pwfile=FILE         read password for the new superuser from file
  -T, --text-search-config=CFG
                            default text search configuration
  -U, --username=NAME       database superuser name
  -W, --pwprompt            prompt for a password for the new superuser
  -X, --waldir=WALDIR       location for the write-ahead log directory
      --wal-segsize=SIZE    size of WAL segments, in megabytes

Less commonly used options:
  -c, --set NAME=VALUE      override default setting for server parameter
  -d, --debug               generate lots of debugging output
      --discard-caches      set debug_discard_caches=1
  -L DIRECTORY              where to find the input files
  -n, --no-clean            do not clean up after errors
  -N, --no-sync             do not wait for changes to be written safely to disk
      --no-instructions     do not print instructions for next steps
  -s, --show                show internal settings
  -S, --sync-only           only sync database files to disk, then exit

Other options:
  -V, --version             output version information, then exit
  -?, --help                show this help, then exit

If the data directory is not specified, the environment variable PGDATA
is used.

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

基本使用

initdb 的基本用法涉及指定要创建新数据库集群的目录。以下是一个简单的命令行示例:

initdb -D /path/to/data_directory

这里的 -D /path/to/data_directory 指定了新数据库集群的数据目录。如果不使用 -D 选项,则 initdb 会使用环境变量 PGDATA 指定的目录,如果 PGDATA 也没有设置,initdb 就会报错。

如之前的初始化过程

[pg16@test ~]$ initdb -D /home/pg16/data -U postgres -k
The files belonging to this database system will be owned by user "pg16".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are enabled.

creating directory /home/pg16/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... America/Los_Angeles
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /home/pg16/data -l logfile start

主要选项

  • -D – 指定数据库集群的数据目录。
  • -E – 设置默认字符编码方式。例如,使用 -E UTF8 来设置 UTF-8 编码。
  • --locale – 用于设置数据库的区域设置,这将影响字符的排序规则和类别行为。
  • --lc-collate, --lc-ctype, --lc-messages, --lc-monetary, --lc-numeric, --lc-time – 这些选项允许你单独设置数据库在不同方面的区域设置,如排序规则、字符分类、消息语言、货币格式、数字格式和日期时间格式。

注意事项

  • 运行 initdb:只有在首次设置 PostgreSQL 安装时才需要手动运行 initdb。大多数时候,当你通过包管理器安装 PostgreSQL 时(例如 aptyumbrew 等),这一步骤会自动为你完成。
  • 权限initdb 必须以 PostgreSQL 服务器将要运行的用户身份来执行,通常这个用户称为 postgres。如果以其他用户身份运行,那么数据库服务器可能无法访问 initdb 创建的文件和目录。
  • 安全性:在初始化数据库时考虑安全设置非常重要。特别是,配置文件 pg_hba.conf 控制客户端认证和连接,应该在初始化之后进行相应的调整以确保数据库的访问安全。
  • 之后操作:一旦使用 initdb 初始化了数据目录,你就可以启动数据库服务器了,通常使用 pg_ctl start 或直接运行 postgres 可执行文件,具体取决于你的系统设置和偏好。

initdb 是 PostgreSQL 安装和配置过程中的关键步骤,正确使用它对于确保数据库集群正确、安全地运行至关重要。

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

相关推荐

  1. PostgreSQL命令工具21- initdb

    2024-05-12 08:34:08       9 阅读
  2. PostgreSQL命令工具02- createdb

    2024-05-12 08:34:08       10 阅读
  3. PostgreSQL命令工具06- pg_isready

    2024-05-12 08:34:08       12 阅读
  4. PostgreSQL命令工具08- pg_restore

    2024-05-12 08:34:08       15 阅读
  5. PostgreSQL命令工具07- pg_dump

    2024-05-12 08:34:08       10 阅读
  6. PostgreSQL命令工具10- pg_basebackup

    2024-05-12 08:34:08       11 阅读
  7. PostgreSQL命令工具14- pg_test_timing

    2024-05-12 08:34:08       11 阅读
  8. PostgreSQL工具介绍

    2024-05-12 08:34:08       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-12 08:34:08       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-12 08:34:08       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-12 08:34:08       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-12 08:34:08       20 阅读

热门阅读

  1. MySQL 数据库中 Insert 语句的锁机制

    2024-05-12 08:34:08       9 阅读
  2. redis

    redis

    2024-05-12 08:34:08      8 阅读
  3. 1.5编程基础之循环控制 03:均值

    2024-05-12 08:34:08       10 阅读
  4. 深入探索Linux权限管理

    2024-05-12 08:34:08       8 阅读
  5. C# winform opencvsharp 识别轮廓。拟合,找到中心点

    2024-05-12 08:34:08       11 阅读
  6. 12350 安全生产举报投诉特服热线系统解决方案

    2024-05-12 08:34:08       9 阅读
  7. 自建 Spark 集群需要考虑以下几个方面的问题

    2024-05-12 08:34:08       12 阅读
  8. 生活中的网络

    2024-05-12 08:34:08       8 阅读