Postgresql: Scripting psql execution with password

后端 未结 16 2419
谎友^
谎友^ 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 19:03

    You have to create a password file: see http://www.postgresql.org/docs/9.0/interactive/libpq-pgpass.html for more info.

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

    An alternative to using PGPASSWORD environment variable is to use conninfo string according to the documentation

    An alternative way to specify connection parameters is in a conninfo string or a URI, which is used instead of a database name. This mechanism give you very wide control over the connection.

    $ psql "host=<server> port=5432 dbname=<db> user=<user> password=<password>"
    
    postgres=>
    
    0 讨论(0)
  • 2020-11-28 19:05

    Use -w in the command: psql -h localhost -p 5432 -U user -w

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

    If you intend on having multiple hosts/database connections, the ~/.pgpass file is the way to go.

    Steps:

    1. Create the file using vim ~/.pgpass or similar. Input your information in the following format: hostname:port:database:username:password Do not add string quotes around your field values. You can also use * as a wildcard for your port/database fields.
    2. You must chmod 0600 ~/.pgpass in order for it to not be silently ignored by psql.
    3. Create an alias in your bash profile that runs your psql command for you. For example:alias postygresy='psql --host hostname database_name -U username' The values should match those that you inputted to the ~/.pgpass file.
    4. Source your bash profile with . ~/.bashrc or similar.
    5. Type your alias from the command line.

    Note that if you have an export PGPASSWORD='' variable set, it will take precedence over the file.

    0 讨论(0)
  • 2020-11-28 19:11
    PGPASSWORD=[your password] psql -Umyuser < myscript.sql
    
    0 讨论(0)
  • 2020-11-28 19:14

    You may find this useful: Windows PSQL command line: is there a way to allow for passwordless login?

    0 讨论(0)
提交回复
热议问题