为了补全,也可以使用 URI (doc link)
列出数据库
psql "postgresql://username:password@localhost/postgres" -l
我还设计了这个只有名字的命令(如果你知道更好的方法,请告诉我):
psql "postgresql://username:password@localhost/postgres" -l | awk -F '|' '{print $1}'| sed -e '/^\s*$/ d' -e '1,3d'|sed '$d'|awk '{print $1}'
你也可以使用unix socket来连接:
# ss -x -a |grep postgres|awk '{print $5}'
/var/run/postgresql/.s.PGSQL.5432
注意使用socket的父目录:
# sudo -u postgres psql -d "postgresql:///postgres?host=/var/run/postgresql/" -l
只有在您的pg_hba.conf 中有此行时,您才能执行此操作:
local all postgres ident
"ident" 使用 unix 用户进行身份验证
转储一个数据库
这里我添加了一个不同的端口号
pg_dump -Fc "postgresql://username:password@localhost:9001/${db}" > "backup_${db}.pgdump"
使用 dumpall 你需要一个超级用户或角色(CREATE ROLE ... SUPERUSER)。它必须有权访问所有数据库。默认postgres可以。
但就我而言,我无法将 pg_dumpall 与 postgres 一起使用,因为他的密码已被开发人员删除。
所以我用了:
sudo -u postgres pg_dumpall -d "postgresql:///?host=/var/run/postgresql/" > all.dump
测试版
# cat /opt/postgresql/PG_VERSION
9.6
第