Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run in-line SQL script in the command line?

I have trouble to execute a SQL script via command line, this is what I have

"C:\..\psql.exe" -h my_server_host -U username -c 'CREATE DATABASE test;'

I got this error:

psql: warning: extra command-line argument "test;'" ignored
psql: FATAL:  database "DATABASE" does not exist

I am on Windows 7 with Postgresql 9.3.

Any idea how to do that?

like image 807
zs2020 Avatar asked Apr 02 '14 22:04

zs2020


2 Answers

You must connect to a database to run a command, even if you want to run CREATE DATABASE, so:

"C:\..\psql.exe" -h my_server_host -U usr -c 'CREATE DATABASE test;' postgres

(As @Craig cleared up, it must be double quotes for Windows.)
Using the default maintenance db postgres here.

There is a better option for the purpose at hand, though: createdb from the command-line directly.

like image 192
Erwin Brandstetter Avatar answered Oct 30 '22 16:10

Erwin Brandstetter


Windows's cmd.exe expects double quotes on arguments, not single quotes.

"CREATE DATABASE test;"

http://blogs.msdn.com/b/oldnewthing/archive/2010/09/17/10063629.aspx

like image 27
Craig Ringer Avatar answered Oct 30 '22 15:10

Craig Ringer