PostgreSQL的 - 从bash脚本数据库用户查询“Postgres的

问题描述 投票:32回答:6

我有一个表,其中有3列我的PostgreSQL数据库 - c_uidc_defaultsc_settingsc_uid简单地存储用户名和c_defaults是一个漫长的一段文字里面包含了很多w.r.t用户数据。

我要执行从一个bash脚本,它选择基于该c_defaultsc_uid列的值的语句,这需要由数据库用户Postgres的实现。

在命令行我能做到以下几点:

[mymachine]# su postgres
bash-4.1$psql
postgres=#\c database_name
You are now connected to database "database_name" as user "postgres".
database_name=#SELECT c_defaults  FROM user_info WHERE c_uid = 'testuser';

但是,我怎么通过一个bash脚本实现这一目标?

其目的是从该列得到的信息,编辑和写回成柱 - 全部通过一个bash脚本。

sql bash postgresql
6个回答
88
投票

试试这个:

#!/bin/bash
psql -U postgres -d database_name -c "SELECT c_defaults  FROM user_info WHERE c_uid = 'testuser'"

或使用su

#!/bin/bash
su -c "psql -d database_name -c \"SELECT c_defaults  FROM user_info WHERE c_uid = 'testuser'\"" postgres

sudo

#!/bin/bash
sudo -u postgres -H -- psql -d database_name -c "SELECT c_defaults  FROM user_info WHERE c_uid = 'testuser'"

20
投票

您可以连接到如下psql的和你一样在区块内的普通Postgres的功能做写你的SQL查询。在那里,可以使用bash的变量。然而,脚本应严格SQL,甚至征求意见,你需要使用 - 而不是#:

#!/bin/bash
psql postgresql://<user>:<password>@<host>/<db> << EOF
       <your sql queries go here>
EOF

14
投票

如果你打算从一个单独的SQL文件运行它。这里是一个很好的例子(从一个伟大的页面带到学习如何与PostgreSQL的http://www.manniwood.com/postgresql_and_bash_stuff/index.html来砸

#!/bin/bash
set -e
set -u
if [ $# != 2 ]; then
   echo "please enter a db host and a table suffix"
   exit 1
fi

export DBHOST=$1
export TSUFF=$2
psql \
  -X \
  -U user \
  -h $DBHOST \
  -f /path/to/sql/file.sql \
  --echo-all \
  --set AUTOCOMMIT=off \
  --set ON_ERROR_STOP=on \
  --set TSUFF=$TSUFF \
  --set QTSTUFF=\'$TSUFF\' \
   mydatabase

   psql_exit_status = $?

   if [ $psql_exit_status != 0 ]; then
     echo "psql failed while trying to run this sql script" 1>&2
     exit $psql_exit_status
   fi

   echo "sql script successful"
exit 0

8
投票

一旦你登录的postgres,你应该能够写:

psql -t -d database_name -c $'SELECT c_defaults FROM user_info WHERE c_uid = \'testuser\';'

打印出的那场,这意味着您可以在捕捉到(例如)保存在击变的只是价值:

testuser_defaults="$(psql -t -d database_name -c $'SELECT c_defaults FROM user_info WHERE c_uid = \'testuser\';')"

为了处理作为postgres,我建议使用sudo记录。你可以给一个特定的用户运行许可

sudo -u postgres /path/to/this/script.sh

使他们可以只运行一个脚本postgres


2
投票

通过在命令脚本psql最安全的方式是通过管道字符串或传递这里-doc的。

该名男子文档的-c/--command选项进入更多细节时,应避免它。

   -c command
   --command=command
       Specifies that psql is to execute one command string, command, and then exit. This is useful in shell scripts. Start-up files (psqlrc and ~/.psqlrc)
       are ignored with this option.

       command must be either a command string that is completely parsable by the server (i.e., it contains no psql-specific features), or a single
       backslash command. Thus you cannot mix SQL and psql meta-commands with this option. To achieve that, you could pipe the string into psql, for
       example: echo '\x \\ SELECT * FROM foo;' | psql. (\\ is the separator meta-command.)

       If the command string contains multiple SQL commands, they are processed in a single transaction, unless there are explicit BEGIN/COMMIT commands
       included in the string to divide it into multiple transactions. This is different from the behavior when the same string is fed to psql's standard
       input. Also, only the result of the last SQL command is returned.

       Because of these legacy behaviors, putting more than one command in the -c string often has unexpected results. It's better to feed multiple
       commands to psql's standard input, either using echo as illustrated above, or via a shell here-document, for example:

           psql <<EOF
           \x
           SELECT * FROM foo;
           EOF

0
投票

为了和@Jason的问题,在我的bash脚本,我做了这样的事情(我的目的):

dbPass='xxxxxxxx'
.....
## Connect to the DB
PGPASSWORD=${dbPass} psql -h ${dbHost} -U ${myUsr} -d ${myRdb} -P pager=on --set AUTOCOMMIT=off

这样做的另一种方式是:

psql --set AUTOCOMMIT=off --set ON_ERROR_STOP=on -P pager=on \
     postgresql://${myUsr}:${dbPass}@${dbHost}/${myRdb}

但你必须非常小心的口令:我不能让一个'和/或:在上班途中密码。所以最终放弃了。

-S

© www.soinside.com 2019 - 2024. All rights reserved.