Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql: Scripting psql execution with password

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

This is what I have:

psql -Umyuser < myscript.sql 

However, I couldn't find the argument that passes the password, and so psql always prompts for it.

like image 627
Axel Fontaine Avatar asked Jun 29 '11 15:06

Axel Fontaine


People also ask

What is password of psql?

Login and Connect as Default User For most systems, the default Postgres user is postgres and a password is not required for authentication. Thus, to add a password, we must first login and connect as the postgres user. $ sudo -u postgres psql.

What is the psql command to change the password?

Log in to psql using the postgres database login role, connecting to the postgres database. Issue the \password command to alter the passwords of the three login roles. The syntax for the \password command is \password <username>. You will be prompted to type a new password.


1 Answers

You may wish to read a summary of the ways to authenticate to PostgreSQL.

To answer your question, there are several ways provide a password for password-based authentication:

  1. Via the password prompt. Example:

    psql -h uta.biocommons.org -U foo Password for user foo:  
  2. In a pgpass file. See libpq-pgpass. Format:

    <host>:<port>:<database>:<user>:<password> 
  3. With the PGPASSWORD environment variable. See libpq-envars. Example:

    export PGPASSWORD=yourpass psql ...  # Or in one line for this invocation only: PGPASSWORD=yourpass psql ... 
  4. In the connection string The password and other options may be specified in the connection string/URI. See app-psql. Example:

    psql postgresql://username:password@dbmaster:5433/mydb?sslmode=require 
like image 115
Reece Avatar answered Oct 19 '22 05:10

Reece