数据库教程:PostgreSQL忘记postgres账号密码的解决方法

postgresql简介postgresql是一个功能非常强大的、源代码开放的客户/服务器关系型数据库管理系统(rdbms)。postgresql最初设想于1986年,当时被叫做berkley pos


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前最好做一个备份,这是一个良好的习惯,避免回滚的时候,你能轻松回撤所有操作。

  #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

trust表示允许可信的本地连接。此时连接数据库不用输入密码。

小知识:

type表示主机类型,它的取值有下面这些:

local :表示是unix-domain的socket连接

host :表示tcp/ip socket

hostssl: 表示ssl加密的tcp/ip socket

如果你打算以tcp/ip方式访问数据库,即psql -h127.0.0.1 -upostgres这样的方式,那么必须修改host的配置。具体如下所示:

  修改前:  # ipv4 local connections:  host all all 127.0.0.1/32 md5        修改后:  # ipv4 local connections:  host all all 127.0.0.1/32 trust

3: 重启postgresql服务

重启postgresql服务的方法很多,这里不做过多介绍,选择你常用的方式即可。

  # service postgresql-9.5 stop  redirecting to /bin/systemctl stop postgresql-9.5.service  # service postgresql-9.5 start  redirecting to /bin/systemctl start postgresql-9.5.service  # service postgresql-9.5 status  redirecting to /bin/systemctl status postgresql-9.5.service

4:重置账号postgres的密码

使用psql无密码登录,修改用户postgres的密码

  alter user postgres with password '新的密码';

方式1:

  #psql -u postgres

方式2:

  #psql

关于两者,如果ssh是用postgres用户连接服务器的话,那么直接psql即可,如果是root用户连接服务器的话,必须用psql -u postgres

  -bash-4.2$ psql  psql (9.5.6)  type "help" for help.     postgres=# alter user postgres with password 'xxxxxxxx';  alter role

5:恢复pg_hba.conf配置文件的修改

6:重启postgresql服务

到此这篇关于postgresql忘记postgres账号的密码的解决方法的文章就介绍到这了,更多相关postgresql忘记postgres密码内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多数据库技术:PostgreSQL忘记postgres账号密码的解决方法,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/632610.html

(0)
上一篇 2021年5月31日
下一篇 2021年5月31日

精彩推荐