postgres使用,备份和恢复

创建一个 PostgreSQL 用户

createuser  username -P

创建数据库

createdb dbname -O username -E UTF8 -e

连接数据库

psql -U username -d dbname -h 127.0.0.1

查看角色权限:\du

超级用户创建角色权限:create role replica Replication;(replica角色,Replication权限)

修改角色登录密码:alter role replica password 'replica';

修改角色权限:alter role replica login;

重启:pg_ctl restart -D 路径(指定数据目录路径)

参考:https://blog.csdn.net/qq_31943653/article/details/76962278

大坑:无法停止postgres服务

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

解决方法:https://www.jianshu.com/p/107235684ad0




postgres数据库的备份和还原

1、备份数据库

pg_dump yoyo -h localhost -U postgres>yoyo_0815.sql

2、停掉数据库连接

psql -h 127.0.0.1 -U postgres

SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname = 'yoyo' AND pid <> pg_backend_pid();


3.退出登录: \q

4、删除数据库

dropdb -h127.0.0.1 -U postgres yoyo

5、创建数据库

createdb -h127.0.0.1 -U postgres yoyo

6、还原数据库

psql -d yoyo -h 127.0.0.1 -U postgres -f yoyo_0815.sql


ssh [email protected]

你可能感兴趣的:(postgres使用,备份和恢复)