在bash中运行psql查询 [英] run psql query in bash

查看:216
本文介绍了在bash中运行psql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是bash和psql的新手,我想编写一个bash代码,使服务器登录到连接到psql数据库的服务器执行选择查询,然后返回输出。

I am new in bash and psql, I want to write a bash code that login a server the connect to psql database do a select query and then return the output. the set up I have as follow.

名为dbs的服务器,它具有psql安装并正在运行。数据库名称= dname,用户=测试人员密码= 1234
在数据库中有一个名为node_info的表,它具有3个冒号ip,mac,connection_time。
我想在查询
之后运行,从node_info选择ip,connection_time,其中ip = 10.10.10.2;

server named dbs, it has psql install and running. database name = dname, user= tester passwrod = 1234 in the database there is table called node_info it has 3 colons ip, mac, connection_time. I want to run following query select ip, connection_time from node_info where ip=10.10.10.2;

任何提示如何编写Shell脚本的技巧要远程执行此操作?

Any tips how to write a shell script to do this remotely?

推荐答案

如果已正确设置 pg_hba.conf配置文件以实现远程连接,则可以尝试以下操作:

If you have properly set your "pg_hba.conf" configuration file to achieve remote connections, you may try the following:

psql -U tester -h remoteip -p remoteport -d dname -W -c "select ip, connection_time from node_info where ip='10.10.10.2'"

这篇关于在bash中运行psql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆