当前系统环境
[root@localhost data]# cat /etc/redhat-release
CentOS release 6.10 (Final)
安装程序
# Install the repository RPM:
[root@localhost postgresql]# yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-6-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# Install PostgreSQL:
[root@localhost postgresql]# yum install postgresql12-server
# Optionally initialize the database and enable automatic start:
[root@localhost postgresql]# service postgresql-12 initdb
[root@localhost postgresql]# chkconfig postgresql-12 on
[root@localhost postgresql]# service postgresql-12 start
可能是由于我本地网络不太好,在执行第二条命令的时候失败了,出现了请求超时的情况。这个时候我果断的选择了重试,嗯,好了。以后遇到这种情况还是鼓励重试,特别是请求超时的情况。
配置数据库
这个地方有个小坑,主要是我熟悉centos系统。由于需要修改配置文件,所以需要找到程序的安装目录,这个花了我一会儿时间。用了ls -R | grep 'postgresql'
命令来查询,结果没找到。最后无意间在一篇帖子里面看到目录是/var/lib/pgsql/12
下面,好吧,上边的命令确实找不到这个地方。
修改默认密码
- 修改
/var/lib/pgsql/12/data/pg_hda.conf
文件, 将md5
改为trust
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# IPv4 local connections:
#host all all 127.0.0.1/32 md5
host all all 127.0.0.1/32 trust
# IPv6 local connections:
#host all all ::1/128 md5
host all all 127.0.0.1/32 trust
- 重启数据库
[root@localhost postgresql]# service postgresql-12 stop
[root@localhost postgresql]# service postgresql-12 start
- 使用
psql
登录数据库,不需要密码
[root@localhost data]# sudo -u postgres psql
psql (12.3)
输入 "help" 来获取帮助信息.
# 注意末尾的分号,我开始就没有些分号,结果下一行显示 postgres-#,这表示结上一行
postgres=# alter user postgres with password 'foobar';
postgres=# \q
- 将配置文件修改回去,并重启服务默认密码修改就完成了。
允许远程访问
- 修改
/var/lib/pgsql/12/data/pg_hda.conf
文件,修改后如下:
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication all peer
#host replication all 127.0.0.1/32 trust
#host replication all ::1/128 trust
host all all 0.0.0.0/0 md5
注意新增的一行间隔需要使用制表符,否则可能会报:Error connecting to the server:致命错误:没有用于主机“…”,用户“…”,数据库“…”,SSL关闭的pg_hba.conf记录
的错误。
- 修改
/var/lib/pgsql/12/data/postgresql.cong
,将localhost
改为*
:
listen_addresses = '*' # what IP address(es) to listen on;
重启服务即可远程访问了。