linux重置postgres密码并设置远程连接

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

弄完后发现可以远程连上了,教程结束

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值