一:忘记密码
1.修改D:\PostgreSQL\13\data\pg_hba.conf文件(scram-sha-256改为trust,就可以不用密码直接登录了)
2.重启服务
3.用postgres用户登录:psql -h 127.0.0.1 -U postgres
4.alter user postgres with password "mypassword"
二:postgresql修改配置文件实现远程访问
使用psql命令登录数据库的命令为:
-
psql -U dbuser -d exampledb -h 127.0.0.1 -p 5432
修改数据库账户postgres密码
alter user postgres with password '123456';
添加新用户和数据库
- create user xiaozhang with password '123456';
- create database exampledb owner xiaozhang;
- grant all privileges on database exampledb to xiaozhang;
修改监听地址:
打开/etc/postgresql/postgresql.conf (ubuntu下)
将 #listen_addresses = 'localhost' 的注释去掉并改为 listen_addresses = '*'
修改可访问的IP段
打开/etc/postgresql/9.5/main/pg_hab.conf
在末尾添加: host all all 0.0.0.0 0.0.0.0 md5 ,表示运行任何IP连接
重启
- sudo /etc/init.d/postgresql restart
三:备份or恢复
备份
pg_dump –h 127.0.0.1 -p 5432 -U postgres -c –f dbname.sql dbname
pg_dump -h 164.82.233.54 -U postgres databasename > C:\databasename.bak
# 使用如下命令可对全部pg数据库进行备份
pg_dumpall –h 127.0.0.1 –p 5432 -U postgres –c –f db_bak.sql
恢复
windows:
psql -h localhost -U postgres -d new_db -f "C:/emoneysit.bak"
ubuntu:
psql -h localhost -U emmweb -d emmweb < /home/jianghai/Desktop/emmweb.bak
-h:数据库服务器地址;
-p:数据库端口号;
-U:U 大写,表示用户名;
-d:数据库名称;
-f:把备份文件放在哪里;
-c:使得在输出的开始是一个创建数据库本身并且重新连接到被创建的数据库的命令;