0

I am creating a batch file to create a new database in PostgreSQL 10. I used the following code,

echo off
cd "C:\Program Files (x86)\PostgreSQL10円\bin"
psql "dbname=myDB host=localhost user=postgres password=postgres port=5432"
pause

But I get this error,

psql: FATAL: database "myDB" does not exist

I understand this is not the command to create a new database. I also tried the following code,

echo off
cd "C:\Program Files (x86)\PostgreSQL10円\bin"
psql "createdb myDB passfile %APPDATA%\postgresql\pgpass.conf"
pause

This is based on the information in this link. The "pgpass.conf" file has the follwoing text,

localhost:5432:myDB:postgres:postgres

But it asks to enter password. I entered the default password "postgres". But this was the output,

psql: FATAL: password authentication failed for user "User1"

The objective here is to create new database from the batch file without asking for password. How to achieve this?

asked May 30, 2018 at 11:02
5
  • 1
    Surely createdb is also available on Windows? postgresql.org/docs/current/static/app-createdb.html Commented May 30, 2018 at 11:16
  • 1
    createdb is a command line program, not a SQL statement. psql is used to run SQL statements, not to execute a command line program. You need to decide if you want to run a SQL statement or a command line program to do that. Commented May 30, 2018 at 11:34
  • 1
    stackoverflow.com/q/6405127 Commented May 30, 2018 at 11:35
  • I already tried the method mentioned in the links. As mentioned in the question, the objective here is to create new database from the batch file without asking for password. How to achieve this? Commented May 31, 2018 at 21:02
  • 1
    I think your usage of passfile is wrong: postgresql.org/docs/current/static/… Also, you will need -U postgres (or user=postgres, depending on which form of connection parameters you are using) in your createdb command. I'd suggest taking one step at a time, carefully reading some introductory material and the error messages. In this case you could notice that User1 is not the username you want. Commented Jun 19, 2018 at 13:27

2 Answers 2

2

Create a bat file with the following content and execute it:

c:\path\to\postgresql\bin\psql.exe -f C:\path\to\db_create.sql postgresql://user:password@localhost:port/postgres

This is what worked for me.

answered Dec 12, 2018 at 11:48
1

Other way to work for me is:

1.- Create one test.bat file with this content:

@set PGPASSFILE=D:\bash\Test02.conf
@"C:\Program Files\PostgreSQL12円\bin\"createdb -U postgres PEDIDOS-2052021141847 
# all in one line

2.- Create one test.conf file with this content:

*:*:*:postgres:1234
# host:port:database:user:password

3.- Execute .bat file

Paul White
95.4k30 gold badges440 silver badges689 bronze badges
answered May 20, 2021 at 20:45
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.