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

打印 上一主题 下一主题

主题 573|帖子 573|积分 1719

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

  1. 基础信息
  2. OS版本:Red Hat Enterprise Linux Server release 7.9 (Maipo)
  3. DB版本:16.2
  4. pg软件目录:/home/pg16/soft
  5. pg数据目录:/home/pg16/data
  6. 端口:5777
复制代码
initdb 是 PostgreSQL 中的一个工具,用于创建一个新的 PostgreSQL 数据库集群。数据库集群是一组数据库和一些与这些数据库相干的初始化信息,包括设置文件(如 postgresql.conf 和 pg_hba.conf)以及事件日记等。实际上,initdb 主要负责初始化数据库体系的文件体系情况。
通过help检察帮助文档。
  1. [pg16@test ~]$ initdb --help
  2. initdb initializes a PostgreSQL database cluster.
  3. Usage:
  4.   initdb [OPTION]... [DATADIR]
  5. Options:
  6.   -A, --auth=METHOD         default authentication method for local connections
  7.       --auth-host=METHOD    default authentication method for local TCP/IP connections
  8.       --auth-local=METHOD   default authentication method for local-socket connections
  9. [-D, --pgdata=]DATADIR     location for this database cluster
  10.   -E, --encoding=ENCODING   set default encoding for new databases
  11.   -g, --allow-group-access  allow group read/execute on data directory
  12.       --icu-locale=LOCALE   set ICU locale ID for new databases
  13.       --icu-rules=RULES     set additional ICU collation rules for new databases
  14.   -k, --data-checksums      use data page checksums
  15.       --locale=LOCALE       set default locale for new databases
  16.       --lc-collate=, --lc-ctype=, --lc-messages=LOCALE
  17.       --lc-monetary=, --lc-numeric=, --lc-time=LOCALE
  18.                             set default locale in the respective category for
  19.                             new databases (default taken from environment)
  20.       --no-locale           equivalent to --locale=C
  21.       --locale-provider={libc|icu}
  22.                             set default locale provider for new databases
  23.       --pwfile=FILE         read password for the new superuser from file
  24.   -T, --text-search-config=CFG
  25.                             default text search configuration
  26.   -U, --username=NAME       database superuser name
  27.   -W, --pwprompt            prompt for a password for the new superuser
  28.   -X, --waldir=WALDIR       location for the write-ahead log directory
  29.       --wal-segsize=SIZE    size of WAL segments, in megabytes
  30. Less commonly used options:
  31.   -c, --set NAME=VALUE      override default setting for server parameter
  32.   -d, --debug               generate lots of debugging output
  33.       --discard-caches      set debug_discard_caches=1
  34.   -L DIRECTORY              where to find the input files
  35.   -n, --no-clean            do not clean up after errors
  36.   -N, --no-sync             do not wait for changes to be written safely to disk
  37.       --no-instructions     do not print instructions for next steps
  38.   -s, --show                show internal settings
  39.   -S, --sync-only           only sync database files to disk, then exit
  40. Other options:
  41.   -V, --version             output version information, then exit
  42.   -?, --help                show this help, then exit
  43. If the data directory is not specified, the environment variable PGDATA
  44. is used.
  45. Report bugs to <pgsql-bugs@lists.postgresql.org>.
  46. PostgreSQL home page: <https://www.postgresql.org/>
复制代码
基本使用

initdb 的基本用法涉及指定要创建新数据库集群的目录。以下是一个简朴的下令行示例:
  1. initdb -D /path/to/data_directory
复制代码
这里的 -D /path/to/data_directory 指定了新数据库集群的数据目录。如果不使用 -D 选项,则 initdb 会使用情况变量 PGDATA 指定的目录,如果 PGDATA 也没有设置,initdb 就会报错。
如之前的初始化过程
  1. [pg16@test ~]$ initdb -D /home/pg16/data -U postgres -k
  2. The files belonging to this database system will be owned by user "pg16".
  3. This user must also own the server process.
  4. The database cluster will be initialized with locale "en_US.UTF-8".
  5. The default database encoding has accordingly been set to "UTF8".
  6. The default text search configuration will be set to "english".
  7. Data page checksums are enabled.
  8. creating directory /home/pg16/data ... ok
  9. creating subdirectories ... ok
  10. selecting dynamic shared memory implementation ... posix
  11. selecting default max_connections ... 100
  12. selecting default shared_buffers ... 128MB
  13. selecting default time zone ... America/Los_Angeles
  14. creating configuration files ... ok
  15. running bootstrap script ... ok
  16. performing post-bootstrap initialization ... ok
  17. syncing data to disk ... ok
  18. initdb: warning: enabling "trust" authentication for local connections
  19. 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.
  20. Success. You can now start the database server using:
  21.     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 时(例如 apt、yum、brew 等),这一步调会主动为你完成。
  • 权限:initdb 必须以 PostgreSQL 服务器将要运行的用户身份来执行,通常这个用户称为 postgres。如果以其他用户身份运行,那么数据库服务器可能无法访问 initdb 创建的文件和目录。
  • 安全性:在初始化数据库时思量安全设置非常紧张。特殊是,设置文件 pg_hba.conf 控制客户端认证和连接,应该在初始化之后进行相应的调整以确保数据库的访问安全。
  • 之后操作:一旦使用 initdb 初始化了数据目录,你就可以启动数据库服务器了,通常使用 pg_ctl start 或直接运行 postgres 可执行文件,具体取决于你的体系设置和偏好。
initdb 是 PostgreSQL 安装和设置过程中的关键步调,正确使用它对于确保数据库集群正确、安全地运行至关紧张。
服膺:心存敬畏,行有所止。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莱莱

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表