1. create user
yum安装,会自动创建postgres用户,但是,建议安装前,先创建用户
[root@pghost1 ~]$ groupadd -g 1000 postgres
[root@pghost1 ~]$ useradd -g 1000 -u 1000 postgres
[root@pghost1 ~]$ id postgres
uid=1000(postgres) gid=1000(postgres) groups=1000(postgres)
a) user can not root
b) 如果是部署集群,建议配置NTP服务,统一集群中每个节点的操作系统用户的uid和gid
2. create data directory
通常会建立形如/pgdata/9.x/xxx_data, xxx为业务线名
[root@pghost1 ~]$ mkdir -p /pgdata/10/{data,backups,scripts,archive_wals}
权限,initdb也会回收权限
[root@pghost1 ~]$ chown -R postgres.postgres /pgdata/10
[root@pghost1 ~]$ chmod 0700 /pgdata/10/data
3. initial data directory
实例化数据目录使用initdb工具。创建新的数据库目录(目录包括存放数据库数据的目录),创建template1和postgres数据库,初始化该数据库实例的默认区域和字符集编码。
-W参数,所以在初始化的过程中,要求为数据库超级用户创建密码
-bash-4.2$ /usr/pgsql-10/bin/initdb -D /pgdata/10/data -W
| 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 disabled. Enter new superuser password: Enter it again: fixing permissions on existing directory /pgdata/10/data ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting dynamic shared memory implementation ... posix creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok WARNING: enabling "trust" authentication for local connections 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: /usr/pgsql-10/bin/pg_ctl -D /pgdata/10/data -l logfile start |
系统自动创建了template1数据库和postgres数据库,
template1是生成其他数据库的模板,
postgres数据库是一个默认数据库,用于给用户、工具或者第三方应用提供默认数据库。
输出的最后一行还告诉了你如何启动刚才初始化的数据库。
另外,
除了initdb,还可以用pg_ctl工具初始化
[postgres@pghost1 ~]$ /opt/pgsql/bin/pg_ctl init -D /pgdata/10/data -o
至此,数据库目录初始化完成。
------------------------------------------------------------------------------------------------------
yum源安装PostgreSQL,
自动创建/var/lib/pgsql/10目录和它的两个子目录:data目录和backups目录。
通过service postgresql-10 init命令会初始化/var/lib/pgsql/10/data目录作为数据目录。
方便,但定制性不好,不建议
本文介绍了在Linux环境下创建PostgreSQL数据库实例的详细步骤,包括创建用户、数据目录,使用initdb或pg_ctl初始化数据目录,并强调了初始化过程中的权限设置和密码配置。同时,文章提醒读者避免使用yum源安装以获取更高的定制性。
5604

被折叠的 条评论
为什么被折叠?



