PG数据库常用操作
- 不登录数据库执行查询
psql -h ip -U 用户名 -d 数据库名 -p 端口 -f 文件名
说明:ip为数据库所在ip,数据库名为要查询的数据库,端口为连接数据库使用的端口号,文件里为具体的执行sql。
实例:
psql -h xx.xx.xx.xx -U postgres -d test -p 5432 -f /tmp/test.txt
test.txt文件内容为:
select user_code from bdp_auth.public.f_userinfo;
- shell脚本自动交互执行查询
sh test.sh
如下为脚本test.sh内容:
#!/bin/bash
DIR=`pwd`
expect <<EOF
set timeout 30
spawn psql -h 29.44.131.13 -U postgres -d bdp_auth -p 5432 -f ${DIR}/huoyue.txt
expect "*postgres:"
send "postgres\r"
expect eof
EOF
共有条评论 网友评论