7

I want to create a Postgres database using a batch file. Now the normal way of doing this is the following:

"C:\Program Files\PostgreSQL\9.0\bin\createdb.exe" -U Myadmin MydatAbseName

This script above creates a database with the default database parameters. However, I want to create a database with the following parameters, as follows:

   WITH OWNER = Myadmin 
   TEMPLATE = template0 
   ENCODING = 'SQL_ASCII'
   TABLESPACE = pg_default
   LC_COLLATE = 'C'
   LC_CTYPE = 'C'
   CONNECTION LIMIT = -1;

Please tell me how to create a database with the above parameters using Batch files.

Also let me know how to use a .sql file to do the same, like this command-line:

"C:\Program Files\PostgreSQL\9.0\bin\createdb.exe" -U Myadmin -f C:\createDB.sql;
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
PresleyDias
  • 3,657
  • 6
  • 36
  • 62

1 Answers1

22

The client program createdb does not support all those options.
Create a file db_create.sql:

CREATE DATABASE MydatAbseName
   WITH OWNER myadmin 
   TEMPLATE template0
   ENCODING 'SQL_ASCII'
   TABLESPACE  pg_default
   LC_COLLATE  'C'
   LC_CTYPE  'C'
   CONNECTION LIMIT  -1;

Call it:

psql -U postgres postgres -f C:/path/to/db_create.sql

The trick here is to connect to the default maintenance db "postgres" and create the new database from there. I do it with the default superuser named "postgres" in my example.
psql -f executes the SQL commands in the given file.

You could also just execute a single command with psql -c (no file involved):

psql -U postgres postgres -c "CREATE DATABASE MydatAbseName WITH OWNER Myadmin
EMPLATE template ENCODING 'SQL_ASCII' TABLESPACE  pg_default LC_COLLATE  'C'
LC_CTYPE  C' CONNECTION LIMIT  -1"

More on creating a database in the fine manual here and here.
More on psql.


On Windows, it looks something like this:

"C:\Program Files\PostgreSQL\verson_number\bin\psql.exe" -U user -f C:/path/to/db_create.sql postgres

The last "postgres" is the name of the default maintenance db. If you want to use it in a batch file you have to answer a password prompt or connect with a user that is allowed access without providing a password. Basics in chapters The Password File and The pg_hba.conf File of the manual. More here:

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • hello, i tried both the methods ,but i could not get them to work somehow, for the first method im getting message saying C:\>"C:\Program Files\PostgreSQL\9.0\bin\psql.exe" psql -u nansis_admin -f 'C:\ createDB.sql' psql: warning: extra command-line argument "nansis_admin" ignored psql: warning: extra command-line argument "-f" ignored psql: warning: extra command-line argument "'C:\createDB.sql'" ignored Password for user -u: after i enter the password, no database is created. – PresleyDias Nov 21 '11 at 09:24
  • my .sql file has the following content ________________________________________ CREATE DATABASE MydatAbseName WITH OWNER nansis_Admin TEMPLATE template ENCODING 'SQL_ASCII' TABLESPACE pg_default LC_COLLATE 'C' LC_CTYPE 'C' CONNECTION LIMIT -1; __________________________________________-- for the second method, the no database is being created, please help thanks for the quick reply though :) – PresleyDias Nov 21 '11 at 09:25
  • @PresleyDias: you have `psql` twice in your command. Try: `"C:\Program Files\PostgreSQL\9.0\bin\psql.exe" postgres -U nansis_admin -f 'C:\createDB.sql'`. Works only if user `nansis_admin` has the required privileges. – Erwin Brandstetter Nov 21 '11 at 09:38
  • Hey my bad:) but i tried this "C:\Program Files\PostgreSQL\9.0\bin\psql.exe" postgres -U nansis_admin -f 'C:\createDB.sql' but still msg comes on execution of the bat file C:\>"C:\Program Files\PostgreSQL\9.0\bin\psql.exe" postgres -U nansis_admin -f ' C:\createDB.sql' psql: warning: extra command-line argument "nansis_admin" ignored psql: warning: extra command-line argument "-f" ignored psql: warning: extra command-line argument "'C:\createDB.sql'" ignored Password for user -U: nansis_admin is the superuser – PresleyDias Nov 21 '11 at 09:47
  • @PresleyDias: I added some more to my answer. – Erwin Brandstetter Nov 21 '11 at 10:28
  • its still not working, my batch file content. "C:\Program Files\PostgreSQL\9.0\bin\psql.exe" -U nansis_admin -f 'C:\createDB.sql' postgres nansis_admin is the super user – PresleyDias Nov 21 '11 at 10:42
  • and my .sql file content is CREATE DATABASE MydatAbseName WITH OWNER nansis_Admin TEMPLATE template0 ENCODING 'SQL_ASCII' TABLESPACE pg_default LC_COLLATE 'C' LC_CTYPE 'C' CONNECTION LIMIT -1; – PresleyDias Nov 21 '11 at 10:43
  • ____ hello Erwin Brandstetter ur method works, but if i remove the qoutes for this (-f C:\createDB.sql) thanks u very much for your help – PresleyDias Nov 21 '11 at 10:46
  • The batch file Content should be "C:\Program Files\PostgreSQL\9.0\bin\psql.exe" -U nansis_admin -f C:\createDB.sql postgres and the .sql file content should be CREATE DATABASE presley WITH OWNER nansis_Admin TEMPLATE template0 ENCODING 'SQL_ASCII' TABLESPACE pg_default LC_COLLATE 'C' LC_CTYPE 'C' CONNECTION LIMIT -1; – PresleyDias Nov 21 '11 at 10:51
  • @PresleyDias: Cool! :) I removed the single quotes from my example as per your feedback. – Erwin Brandstetter Nov 21 '11 at 11:38
  • Brandstetter : how to prevent the batch file from asking for password? – PresleyDias Nov 22 '11 at 04:47
  • @PresleyDias: added another answer to my answer. – Erwin Brandstetter Nov 22 '11 at 10:17
  • Brandstetter: if i change the port postgres is asking for password for the user while creating the database from the batch file, i cannot figure out how to add the -w --no-password parameter to can you give me full example of it like "C:\Program Files\PostgreSQL\verson_number\bin\psql.exe" -U user -f C:\path\to\db_create.sql postgres -w _______? – PresleyDias Dec 02 '11 at 11:37
  • @PresleyDias: different port means different DB cluster and different pg_hba.conf file. Have you set up password-less access for the user in question? The `-w` option to `psql` only suppresses the password prompt if the user has the privilege to connect without password. – Erwin Brandstetter Dec 02 '11 at 19:40
  • Ok, the user in my case is the superuser so he has all the privileges, so how to suppress the user password prompt for the diff port ? meaning the whole syntax like this "C:\Program Files\PostgreSQL\verson_number\bin\psql.exe" -U user -p 5433 -f C:\path\to\db_create.sql postgres -w" – PresleyDias Dec 05 '11 at 04:44
  • @PresleyDias: There is no such thing as "a different port". A different port means a different database cluster - with its own users and privileges. – Erwin Brandstetter Dec 05 '11 at 04:53
  • ok i may be saying it wrongly...on my system i want to install a fresh copy of postgreSQl 9.0, using a batch file 'C:\temp\Postgres9.0\postgresql-9.0.2-1-windows.exe --serverport '5432' --servicename postgres_service --locale C --superaccount _admin --superpassword k,zii --unattendedmodeui minimal --debuglevel 2 --mode unattended ; ____since postgres has been installed on port 5432....but if i install postgres on port 5435 , during the database creation using your answer above..its is asking for password, – PresleyDias Dec 05 '11 at 07:40
  • For the same procedure above for a fresh install on port 5432 it doesnt ask for password during the database creation through batch file – PresleyDias Dec 05 '11 at 07:41
  • Kenny Evitt's answer at https://stackoverflow.com/questions/2942485/psql-fatal-ident-authentication-failed-for-user-postgres, solved my issue! – AVA Apr 08 '18 at 13:06