postgresql-13 的安装
这篇文章的编写时间是postgresql13.1之后发布的,是最简单粗暴的指导,没有之一。
我已经帮大家踩完坑了,直接按步骤来就行了。
- 先更新安装源,如下:
sudo yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum -y update
sudo yum -y update
- 安装postgresql13
sudo yum -y install postgresql13-server
- 初始化数据库(如果想改变数据库存储位置,可以跳过这步)
$ sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
Initializing database ... OK
#启动服务
$ systemctl start postgresql-13
#允许服务开机运行
$ systemctl enable postgresql-13
postgresql-13 修改初始化数据路径
1. 更改数据存储位置
这里介绍systemctl 的使用如下:
systemctl edit postgresql-13.service
在新空白页覆盖原服务配置
[Service]
Environment=PGDATA=/data/pgdata
这个文件默认放在 /etc/systemd/system/postgresql-13.service.d 目录下。
里面会有 override.conf 文件。
2. 创建文件夹权限问题
当你执行这个命令时候,你会发现数据库创建没有返回‘OK’,我的天啊,而且直接闪退。
$ /usr/pgsql-13/bin/postgresql-13-setup initdb
如果使用 systemctl start postgresql-13 命令的时候你会发现不好使。
其实际原因可以查看 more /var/lib/pgsql/13/initdb.log 的日志,你会发现如下错误:
Data page checksums are disabled.
fixing permissions on existing directory /data/pgdata ... initdb: error: could not change permissions of directory "/data/pgdata": Operation not permitted
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
意思是指这个文件夹不是postgres创建的,所以你要通过postgres创建这个文件夹,就可以了。
$ su - postgres
$ cd /data
$ mkdir ./pgdata
然后你在试下:
$ /usr/pgsql-13/bin/postgresql-13-setup initdb
Create Database.
Ok.
systemctl start postgresql-13.service
systemctl enable postgresql-13.service
貌似没有错误了。
3. 修改postgres默认密码
# 切换到 postgres 用户
$ sudo -u postgres psql
psql (13.1)
Type "help" for help.
postgres=# ALTER USER postgres WITH PASSWORD ‘postgres’;
4. 修改登录方式
首先修改 /var/lib/pgsql/10/data/postgresql.conf
listen_address = '*'
password_encryption = 'md5'
在修改 /var/lib/pgsql/10/data/pg_hba.conf, 如下
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
#host all all 127.0.0.1/32 scram-sha-256
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
但是我发现,nodejs连接postgresql-13 是好使了,但是navicate不行,原因是postgresql-13 改动还是不小的,安全加密策略有变化和完善,现在已经使用scram-sha-256方式了。
没招把md5 改为 password 方式就ok了,也都正常了。如下:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
#host all all 127.0.0.1/32 scram-sha-256
host all all 0.0.0.0/0 password
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident