PostgreSQL忘记postgres账号密码的解决办法

PostgreSQL简介
 
PostgreSQL是一个功能非常强大的、源代码开放的客户/服务器关系型数据库管理系统(RDBMS)。PostgreSQL最初设想于1986年,当时被叫做Berkley Postgres Project。该项目一直到1994年都处于演进和修改中,直到开发人员Andrew Yu和Jolly Chen在Postgres中添加了一个SQL(Structured Query Language,结构化查询语言)翻译程序,该版本叫做Postgres95,在开放源代码社区发放。
 
下面给大家介绍下PostgreSQL忘记postgres密码的处理方法,具体内容如下所示:
 
PostgreSQL数据库中,假如你忘记了postgres账号的密码或者由于工作交接问题,等交接到你手头的时候,没有postgres账号密码,那怎么办呢?其实不用慌,像MySQL、SQL Server等数据库一样,只要你拥有操作系统权限,修改postgres超级账号的密码也非常方便简单。下面测试环境为CentOS Linux release 7.2.1511 (Core), PostgreSQL数据库版本为9.5。其它不同版本的操作其实是一样的,只是略有细微差别。
 
1:定位pg_hba.conf文件位置
 
首先找到pg_hba.conf文件的位置,具体有下面这些方法:
 
方法1:locate定位pg_hba.conf文件的位置
 
$ locate pg_hba.conf
/usr/pgsql-9.5/share/pg_hba.conf.sample
/var/lib/pgsql/9.5/data/pg_hba.conf
 
 
 
方法2:find命令查找。
 
$ find / -name "pg_hba.conf" 2>%1 | grep -v "Permission denied"
/var/lib/pgsql/9.5/data/pg_hba.conf
 
 
 
2:修改pg_hba.conf配置文件
 
修改pg_hba.conf前最好做一个备份,这是一个良好的习惯,避免回滚的时候,你能轻松回撤所有操作。
 
1#cp /var/lib/pgsql/9.5/data/pg_hba.conf /var/lib/pgsql/9.5/data/pg_hba.conf.20210125
 
在pg_hba.conf中找到类似下面这样的地方:
 
# TYPE DATABASE USER ADDRESS  METHOD
 
# "local" is for Unix domain socket connections only
local all all   md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128  md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres  peer
#host replication postgres 127.0.0.1/32 ident
#host replication postgres ::1/128  ident
 
# Allow access from all host to connect to this UAT server
host all all 0.0.0.0/0 md5
 
 
 
关于修改pg_hba.conf,如果你打算以socket方式在本机登录数据库,那么只需修改local这条记录,将pg_hba.conf中的这个选项的的值从md5修改为trust
 
修改前
# "local" is for Unix domain socket connections only
local all all   md5
 
修改后
# "local" is for Unix domain socket connections only
local all all   trust
【声明】:芜湖站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

相关文章