Centos7/8快速安装PostgreSQL与配置
Posted 境悟初
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7/8快速安装PostgreSQL与配置相关的知识,希望对你有一定的参考价值。
查看可安装版本
# yum module list | grep postgresql
Failed to set locale, defaulting to C.UTF-8
postgresql 9.6 client, server [d] PostgreSQL server and client module
postgresql 10 [d] client, server [d] PostgreSQL server and client module
postgresql 12 client, server [d] PostgreSQL server and client module
postgresql 13 client, server [d] PostgreSQL server and client module
安装
# yum install @postgresql:10
初始化数据库
# postgresql-setup --initdb
* Initializing database in '/var/lib/pgsql/data'
* Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
配置外网可访问
默认只有localhost
可访问。
修改配置文件:/var/lib/pgsql/data/postgresql.conf
listen_addresses = '*'
再修改 pg_hba.conf
,增加
host all all 0.0.0.0/0 md5
要重启:
# systemctl restart postgresq
修改数据库用户密码
# su - postgres
Last login: Thu Feb 17 08:48:01 UTC 2022 on pts/1
[postgres@ip-10.1.1.3 ~]$ psql
psql (10.17)
Type "help" for help.
postgres=# alter user postgres with password 'passssssss';
ALTER ROLE
postgres=# \\q
要指定数据目录怎么做?
默认情况下,是存在 /var/lib/pgsql/data/
目录的,但这一般是系统盘,空间不够。
那么可以 使用initdb命令初始化目录。
# su - postgres
Last login: Thu Feb 17 08:51:52 UTC 2022 on pts/0
[postgres@ip-172-31-32-49 ~]$ cd /dev/shm/
[postgres@ip-172-31-32-49 shm]$ ll
total 0
drwxr-xr-x. 2 postgres postgres 40 Feb 17 09:42 pg_data
# 使用initdb命令初始化目录
[postgres@ip-172-31-32-49 shm]$ initdb -D /dev/shm/pg_data/
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 /dev/shm/pg_data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... UTC
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:
pg_ctl -D /dev/shm/pg_data/ -l logfile start
然后再启动
pg_ctl -D /dev/shm/pg_data/ -l logfile start
如果要迁移已有数据目录,可以参考文章:https://www.digitalocean.com/community/tutorials/how-to-move-a-postgresql-data-directory-to-a-new-location-on-ubuntu-16-04
以上是关于Centos7/8快速安装PostgreSQL与配置的主要内容,如果未能解决你的问题,请参考以下文章