ssh登录linux后重置postgresql密码
1、备份配置文件pg_hba.conf
我们需要对pg-hba.conf修改,在改动前最好先备份它。它一般在/etc/postgresql/11/main/pg_hba.conf。
$ cd /etc/postgresql/11/main/
$ cp pg_hba.conf pg_hba.conf-backup
2、修改配置文件pg_hba.conf
$ vi pg_hba.conf
改变以下内容:
# IPv4 local connections:
host all all 127.0.0.1/32 md5
改变后:
# IPv4 local connections:
host all all 127.0.0.1/32 trust
:wq 退出并保存
3、重启postgresql
$ sudo /etc/init.d/postgresql restart
或者
$ systemctl restart postgresql
4、使用超级用户root 或者postgres重置密码。
重置密码为mypwd (自己取)
$ psql -U postgres
$ postgres=# alter user postgres with password 'mypwd';
$ postgres=# \q
5、恢复原来的配置
完成重置密码后,记得要恢复原来的配置文件,因为使用trust启动是无需密码登录的。
$ cp pg_hba.conf-backup pg_hba.conf
完成恢复后,需重启服务器
$ sudo /etc/init.d/postgresql restart
6、配置远程访问用Navicat软件连接并操作数据库
查看进程:
$ ps -ef | grep postgres
看状态:
$ service postgresql status
设置远程访问
修改配置文件postgresql.conf
一般在 /etc/postgresql/11/main/postgresql.conf
这个文件很长,vim后按方向键上键滚到上面
有一行是
# listen_addresses='localhost'
将它修改为
listen_addresses = '*'
修改配置文件pg_hba.conf
一般在 /etc/postgresql/11/main/pg_hba.conf
...
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
...
添加一条 IP 授权记录,0.0.0.0/0 代表所有 ip 都可以访问
注意任意ip访问的验证方式是md5,千万不要设为trust
trust代表只验证用户名不验证密码,会导致任意密码都可以登录成功
设置完需要重启数据库才能生效
sudo /etc/init.d/postgresql restart
弄完后发现可以远程连上了,教程结束