HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlModerate

How to prevent asking for password when creating new database in PostgreSQL 10?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlpreventnewcreatingpassworddatabaseforhowaskingwhen

Problem

I use the following batch file to create a new database in PostgreSQL 10.

echo off
cd "C:\Program Files (x86)\PostgreSQL\10\bin"
createdb -h localhost -p 5432 -U postgres myDB


But it asks to enter the password. How to prevent asking for the password? I tried the -w option explained in this link to not ask for password prompt. But this did not work. How to prevent asking for password?

Solution

use the PGPASSWORD environment variable,

@SET PGPASSWORD=something_secret
psql -c "CREATE DATABASE mydb" -U postgres postgres


or just use a connection string

psql -c "CREATE DATABASE mydb" "user=postgres dbname=postgres password=something_secret"

Code Snippets

@SET PGPASSWORD=something_secret
psql -c "CREATE DATABASE mydb" -U postgres postgres
psql -c "CREATE DATABASE mydb" "user=postgres dbname=postgres password=something_secret"

Context

StackExchange Database Administrators Q#209147, answer score: 11

Revisions (0)

No revisions yet.