PostgreSQL:初始化数据库 (转载)

原文地址: https://blog.csdn.net/fengbohello/article/details/115115162

一、初始化数据库的命令

#!/bin/bash
 
adduser postgres
 
PGHOME="/opt/common/postgresql" # PostgreSQL 命令的位置
datadir="/opt/data/pgdata-13.1" # 数据库文件的位置,在执行这个脚本前,这个目录必须不存在
mkdir -p ${datadir} # 创建目录结构
 
chown postgres:postgres ${datadir} -R
 
su - postgres -c "${PGHOME}/bin/initdb -D ${datadir} -E UTF8 --locale=C -U postgres" # 初始化数据库文件
 
su - postgres -c "${PGHOME}/bin/pg_ctl -D ${datadir} start" # 启动数据库
 
password=$(cat /proc/sys/kernel/random/uuid) # 生成一个随机密码
${PGHOME}/bin/psql -U postgres -h 127.0.0.1 -c "alter role postgres with password '${password}'" # 把数据库用户 postgres 的密码改为刚刚生成的随机密码
 
psql -U postgres -h 127.0.0.1 -p 5432 -c "create database dogdb with template = template0 ENCODING = UTF8" # 创建一个名为 dogdb 的数据库
 
echo "pg user postgres, password [${password}]" # 输出这个数据库密码

二、执行脚本,生成数据库文件

$ ./init-db.sh 

adduser: user 'postgres' already exists

The files belonging to this database system will be owned by user "postgres".

This user must also own the server process.

The database cluster will be initialized with locale "C".

The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /opt/data/pgdata-13.1 ... ok

creating subdirectories ... ok

selecting dynamic shared memory implementation ... posix

selecting default max_connections ... 100

selecting default shared_buffers ... 128MB

selecting default time zone ... UTC

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

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:

/opt/common/postgresql/bin/pg_ctl -D /opt/data/pgdata-13.1 -l logfile start

waiting for server to start....2021-03-23 03:50:39.694 UTC [13712] LOG: starting PostgreSQL 13.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit

2021-03-23 03:50:39.695 UTC [13712] LOG: listening on IPv6 address "::1", port 5432

2021-03-23 03:50:39.695 UTC [13712] LOG: listening on IPv4 address "127.0.0.1", port 5432

2021-03-23 03:50:39.702 UTC [13712] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"

2021-03-23 03:50:39.710 UTC [13713] LOG: database system was shut down at 2021-03-23 03:50:39 UTC

2021-03-23 03:50:39.716 UTC [13712] LOG: database system is ready to accept connections

done

server started

ALTER ROLE

CREATE DATABASE

pg user postgres, password [422fcc44-c85d-44ba-aa16-d899ac59e9b3]

从输出的信息可以看出,创建数据库成功,并且随机密码是:422fcc44-c85d-44ba-aa16-d899ac59e9b3

查看数据库创建结果:

$ /opt/common/postgresql/bin/psql -h 127.0.0.1 -U postgres -c "\l"
                             List of databases
                             
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 dogdb     | postgres | UTF8     | C       | C     | 
 postgres  | postgres | UTF8     | C       | C     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(4 rows)

可以看到 dogdb 已经创建成功。
posted @ 2022-05-09 08:26  HSping  阅读(1040)  评论(0编辑  收藏  举报