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

后端 未结 8 1214
[愿得一人]
[愿得一人] 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:57

    In my case the best solution including the requirement for authentication is:

    username="admin"
    new_password="helloworld"
    
    PGPASSWORD=DB_PASSWORD \
      psql -h HOSTNAME -U DB_USERNAME -d DATABASE_NAME -c \
      "UPDATE user SET password = '$new_password' WHERE username = '$username'"
    

    This command will update a password of a user e.g. for recovery case.

    Info: The trade-off here is that you need to keep in mind that the password will be visible in the bash history. For more information see here.

    Update: I'm running the databse in a docker container and there I just need the commmand: docker exec -i container_name psql -U postgres -d postgres -c "$SQL_COMMAND"

提交回复
热议问题