73

c_uidPostgreSQL データベースに、 、 、c_defaultsおよびの3 つの列を持つテーブルがありますc_settingsc_uidユーザーの名前を保存するだけで、c_defaultsそのユーザーに関する多くのデータを含む長いテキストです。

c_defaults値に基づいて列の値を選択する bash スクリプトからステートメントを実行するc_uid必要があり、これはデータベース ユーザー「postgres」が実行する必要があります。

CLI では、次のことができます。

[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 スクリプトを使用します。

4

9 に答える 9

143

これを試してください:

#!/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'"
于 2013-08-14T05:32:28.540 に答える
17

別の sql ファイルから実行する予定がある場合。ここに良い例があります(postgresqlでbashする方法を学ぶための素晴らしいページから取られました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
于 2015-05-19T19:46:09.850 に答える
12

としてログインするとpostgres、次のように記述できるはずです。

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

そのフィールドの値だけを出力します。つまり、(たとえば) Bash 変数に保存するためにキャプチャできます。

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

として 1 つのスクリプトだけを実行できるようにしpostgresます。

于 2013-08-14T05:29:33.960 に答える
3

スクリプトでコマンドを渡す最も安全な方法psqlは、文字列をパイプするか、ヒアドキュメントを渡すことです。

オプションのマニュアル ドキュメントでは、-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
于 2016-06-23T11:31:36.307 に答える