Postgresql: Scripting psql execution with password

后端 未结 16 2418
谎友^
谎友^ 2020-11-28 18:54

How can I call psql so that it doesn\'t prompt for a password?

This is what I have:

psql -Umyuser < myscript.sql         


        
相关标签:
16条回答
  • 2020-11-28 18:55

    This also works for other postgresql clis for example you can run pgbench in non-interactive mode.

    export PGPASSWORD=yourpassword
    /usr/pgsql-9.5/bin/pgbench -h $REMOTE_PG_HOST -p 5432 -U postgres -c 12 -j 4 -t 10000 example > pgbench.out 2>&1 &
    
    0 讨论(0)
  • 2020-11-28 18:59

    You can add this command line at the begining of your script:

    set PGPASSWORD=[your password]
    
    0 讨论(0)
  • 2020-11-28 19:00

    I find, that psql show password prompt even you define PGPASSWORD variable, but you can specify -w option for psql to omit password prompt.

    0 讨论(0)
  • 2020-11-28 19:02

    Given the security concerns about using the PGPASSWORD environment variable, I think the best overall solution is as follows:

    1. Write your own temporary pgpass file with the password you want to use.
    2. Use the PGPASSFILE environment variable to tell psql to use that file.
    3. Remove the temporary pgpass file

    There are a couple points of note here. Step 1 is there to avoid mucking with the user's ~/.pgpass file that might exist. You also must make sure that the file has permissions 0600 or less.

    Some have suggested leveraging bash to shortcut this as follows:

    PGPASSFILE=<(echo myserver:5432:mydb:jdoe:password) psql -h myserver -U jdoe -p 5432 mydb
    

    This uses the <() syntax to avoid needing to write the data to an actual file. But it doesn't work because psql checks what file is being used and will throw an error like this:

    WARNING: password file "/dev/fd/63" is not a plain file
    
    0 讨论(0)
  • 2020-11-28 19:02

    8 years later...

    On my mac, I had to put a line into the file ~/.pgpass like:

    <IP>:<PORT>:<dbname>:<user>:<password>
    

    Also see:
    https://www.postgresql.org/docs/current/libpq-pgpass.html
    https://wiki.postgresql.org/wiki/Pgpass

    0 讨论(0)
  • 2020-11-28 19:03

    There are several ways to authenticate to PostgreSQL. You may wish to investigate alternatives to password authentication at https://www.postgresql.org/docs/current/static/client-authentication.html.

    To answer your question, there are a few ways provide a password for password-based authentication. The obvious way is via the password prompt. Instead of that, you can provide the password in a pgpass file or through the PGPASSWORD environment variable. See these:

    • https://www.postgresql.org/docs/9.0/static/libpq-pgpass.html
    • https://www.postgresql.org/docs/9.0/interactive/libpq-envars.html

    There is no option to provide the password as a command line argument because that information is often available to all users, and therefore insecure. However, in Linux/Unix environments you can provide an environment variable for a single command like this:

    PGPASSWORD=yourpass psql ...
    
    0 讨论(0)
提交回复
热议问题