PostgreSQL - query from bash script as database user 'postgres'

后端 未结 8 1159
[愿得一人]
[愿得一人] 2020-12-22 19:22

I have a table in my PostgreSQL database which has 3 columns - c_uid, c_defaults and c_settings. c_uid simply stores the

8条回答
  •  执笔经年
    2020-12-22 19:51

    Once you're logged in as postgres, you should be able to write:

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

    to print out just the value of that field, which means that you can capture it to (for example) save in a Bash variable:

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

    To handle the logging in as postgres, I recommend using sudo. You can give a specific user the permission to run

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

    so that they can run just the one script as postgres.

提交回复
热议问题