1

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?

  • 1
    Does it ask you when you just try to database as postgres user? – Mladen Uzelac Jun 8 '18 at 20:26
  • Yes it does ask. – Codename K Jun 8 '18 at 20:52
  • 1
    Do you know how to create pgpass file? Or to manage pg_hba.conf file? – Mladen Uzelac Jun 8 '18 at 21:11
  • I tried the pgpass file method but it did not work. But how does "manage pg_hba.conf" file method work? – Codename K Jun 8 '18 at 21:20
  • 1
    I'll answer it tomorrow – Mladen Uzelac Jun 8 '18 at 21:34
2

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"
  • I see that you have given two database names, mydb and postgres (dbname=postgres). Does this mean the database mydb comes under postgres? – Codename K Jun 9 '18 at 16:34
  • @CodenameK: to create a database (mydb) you must first be connected to another database (postgres in the above example). It does not imply a hierarchy of databases. – Daniel Vérité Jun 9 '18 at 18:20
2

You may enter this line in the pg_hba.conf file like this:

# TYPE DATABASE USER ADDRESS METHOD local all postgres peer

This is for running commands from the machine. This passwordless login from local computers only If you are on Windows any user can put a switch -U postgres and connect to the machine as superuser. Do that only for development purposes.

1

Your problem is that you are using TCP connection and not socket connection.

I just tried on Windows 7 this command and work normally: createdb -U postgres test I hope that you notice that there is no -h localhost Since -p 5432 is default you can omit that also. I checked that pg_hba.conf doesn't have the configuration using peer but it works anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.