二、PostgreSQL初始化配置&启动

1.数据库初始化

$ initdb --pgdata=/var/postgre/data  --encoding=UTF8  --locale=en_US.utf8

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 "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.

fixing permissions on existing directory /var/postgre/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 ... PRC

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:

    pg_ctl -D /var/postgre/data -l logfile start

2.启动数据库

$ pg_ctl -D /var/postgre/data -l logfile start

3.PostgreSQL 配置参数修改

(1) 允许所有主机连接:pg_hba.conf 最后添加一行

$ cd $PGDATA

$ vi pg_hba.conf

host    all             all             0.0.0.0/0               md5

(2) postgresql.conf参数配置修改及打开

cat postgresql.conf   | sed -n '/^#/!p'  | sed '/^$/d'     /*打印非#开头的行,删除空白行*/

listen_addresses = '*'            # 设置监听的主机名或 IP 地址;

max_connections = 1500                  # 设置并发联接的最大个数。如果设定值为default,表示该参数随内存规格变化。

unix_socket_directories = '/tmp'        # 设置用于创建Unix-domain套接字的目录。

unix_socket_permissions = 0700          # 设置 Unix-domain 套接字的访问权限。

password_encryption = md5               # 加密口令。

shared_buffers = 1GB                    # min 128kB ,设置服务器使用的共享内存缓冲区的块数量,每一块大小为8kB。如果设定值为default,表示该参数随内存规格变化。管理进程、操作系统本身也会占用内存,为保证数据库功能正常运行,因而将该参数的最大值限定为内存的80%。

maintenance_work_mem = 2GB              # min 1MB 设置维护操作使用的最大内存数。如果设定值为default,表示该参数随内存规格变化。

wal_level = logical                     # minimal, replica, or logical,设置写入WAL文件的信息的内容详细级别。

max_wal_size = 2GB                      #设置触发一次检查点的WAL尺寸。

min_wal_size = 512MB                     #设置要把WAL收缩到的最小尺寸。

archive_mode = on               # enables archiving; off, on, or always

archive_command = 'test ! -f /pgdata/archivelog/%f && cp %p /pgdata/archivelog/%f'              # 设置用于对WAL文件进行归档的shell命令。

archive_timeout = 3600          # force a logfile segment switch after this    如果新的文件没有在N秒内启动,那么强制切换到下一个xlog文件。

max_wal_senders = 6             # max number of walsender processes 设置同时运行的WAL发送进程最大数量。

wal_keep_segments = 4           # in logfile segments; 0 disables 设置用于备用服务器而持有WAL文件的数量。

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log' # log file name pattern,  设置日志文件的文件名字模式。

log_file_mode = 0600                    # creation mode for log files, 设置日志文件的文件访问权限。

lc_messages = 'en_US.UTF-8'                     # locale for system error message 设置信息显示语言。

lc_monetary = 'en_US.UTF-8'                     # locale for monetary formatting  为货币数量格式设置 locale。

lc_numeric = 'en_US.UTF-8'                      # locale for number formatting   为数字格式设置 locale。

lc_time = 'en_US.UTF-8'                         # locale for time formatting  为日期和时间值格式设置 locale。

4.PostgreSQL 服务启动

(1)将软件解压包中的linux拷贝到/etc/init.d/目录下,并重命名为postgresql

# cp /var/postgre/software-iso/postgresql-14.1/contrib/start-scripts/linux  /etc/init.d/postgresql

(2)修改权限

# chmod  755 /etc/init.d/postgresql

(3)修改配置信息

# vi  /etc/init.d/postgresql

prefix=/var/postgre/14.1      #postgres软件安装位置

PGDATA="/var/postgre/data"      #postgres数据文件存放位置

(4)启动服务

# systemctl enable postgresql

# systemctl start postgresql

5. PostgreSQL psql登录DB检查

# su - postgres

$ psql

psql (14.1)

Type "help" for help.

postgres=# \l

                                  List of databases

   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges

-----------+----------+----------+-------------+-------------+-----------------------

 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +

           |          |          |             |             | postgres=CTc/postgres

 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +

           |          |          |             |             | postgres=CTc/postgres

(3 rows)

postgres=#

https://www.pgadmin.org/download/          pgadmin版本下载
https://www.modb.pro/db/112493               图形化界面工具之pgAdmin4

你可能感兴趣的:(postgresql,数据库)