1

I'm a newbie to Ruby and web development. I'm using Windows 7 64-bit with Ruby 2.0 and I have PostgreSQL 9.4 installed.

I'm trying to use ActiveRecord to create a database. I checked that my postgresql server is running and I did bundle install to make sure I had all the required gems. When I run "bundle exec rake db:create," it looks like rake is able to find createdb.exe in the PostgreSQL directory, but for some reason it's telling me that I have too many command line arguments:

C:\Users\MH\Desktop\activerecord-template> bundle exec rake db:create
Creating activerecord-template development and test databases if they don't exist...
'createdb': too many command line-arguments (first is "postgres")

My Gemfile, Rakefile, and config.rb file are here in my prior post: createdb not recognized as a command when using ActiveRecord

I think the issue might be in the Rakefile here:

desc "Create #{APP_NAME} databases"
      task "create" do
        puts "Creating #{APP_NAME} development and test databases if they don't exist..."
        system("createdb #{DB_NAME} --username #{DB_USERNAME} -w --no-password && createdb #{TEST_DB_NAME} --username #{DB_USERNAME} -w --no-password")
      end

I tried deleting and changing the order of -w as referenced here: pg_dump: too many command line arguments, but that didn't fix the issue.

There was also some discussion about putting "your options before your option-less arguments" and the wrong order for options in these links:

http://grokbase.com/t/postgresql/pgsql-general/07avnzajn7/createdb-argument-question

http://postgresql.nabble.com/pgpass-does-not-work-for-createlang-td2118667.html

but I don't really get what that means for my code.

Community
  • 1
  • 1
user4992261
  • 31
  • 1
  • 9

3 Answers3

0

Since you are on Windows, there may be an issue with the syntax of the line:

system("createdb #{DB_NAME} --username #{DB_USERNAME} -w --no-password && createdb #{TEST_DB_NAME} --username #{DB_USERNAME} -w --no-password")

If Ruby uses the default Windows cmd.exe to run the command, the && operator won't work.

I would probably break it up into 2 statements:

ok   = system("createdb #{DB_NAME} --username #{DB_USERNAME} -w --no-password")
ok &&= system("createdb #{TEST_DB_NAME} --username #{DB_USERNAME} -w --no-password")
# ...can check "ok" for success of both calls if you like...

Finally, if the problem persists, then there must be another createdb executable in the PATH which is hit before your Postgres binaries path. You could try system("path") and see what the output is, and check those dirs for a conflicting createdb.

lilole
  • 322
  • 2
  • 9
  • I just thought also, you should make sure your variable names all contain some value, and none are empty. You could set a var and output before the `system` call: `cmd = "createdb #{DB_NAME} --username #{DB_USERNAME} -w --no-password"; puts cmd` – lilole Jun 10 '15 at 23:23
  • I suspect I need to break up the system() line as well but when I tried to input your suggested changes it shows me syntax error in the terminal with the equals sign. It says the equals signs are "unexpected." I think Windows can take && but I don't know what it means by "too many" arguments. Is there a way to find out the max # of arguments createdb can take? – user4992261 Jun 10 '15 at 23:34
  • Syntax error, that's a surprise. What is the full error text? What do you mean "in the terminal"? You're right, Windows 7 cmd.exe does seem happy with the `&&`, so that would suggest some "wrong" `createdb` being called. – lilole Jun 10 '15 at 23:38
  • I meant in the command terminal by running bundle exec rake db:create in cmd.exe. I suspect the option flags must be written incorrectly. Looking at this reference: http://www.commandprompt.com/ppbook/x17149, if I have something like --username, it's supposed to be --username = #{DB_NAME} and not have the space like that. It looks like the syntax is mixed up between the single flags and double flags. I'll try rewriting it some more. I think I can keep && but I need to edit the flags so createdb understands the syntax. – user4992261 Jun 10 '15 at 23:49
  • Got it! This is the correct syntax: system("createdb #{DB_NAME} --username=#{DB_USERNAME} && createdb #{TEST_DB_NAME} --username=#{DB_USERNAME}"). For some reason, that method doesn't like having any option flags for passwords so that must be completely removed. In addition, whoever wrote this file didn't put in the correct equals signs to use with the --username option flags so createdb.exe was getting hung up on that. The #{DB_USERNAME} string wasn't supposed to be another argument but without the equals sign, createdb.exe was looking at it as a separate argument. – user4992261 Jun 10 '15 at 23:57
  • Congrats. Nice debugging. – lilole Jun 11 '15 at 00:05
0

The Rakefile "create" task uses createdb.exe from PostgreSQL. system() takes in options for createdb.exe. Those options are described in these resources:

For this specific Rakefile, the option flags are incorrectly formatted so createdb.exe is not able to understand the command-line arguments it is being given. Specifically, single option flags and double option flags are being used together incorrectly:

  • --username must be set equal to a value
  • -w and --no-password mean the same thing and are redundant. You should use one or the other if you are going to use this option flag, but not both.

The correct syntax for system() in this Rakefile is:

system("createdb #{DB_NAME} --username=#{DB_USERNAME} && createdb #{TEST_DB_NAME} --username=#{DB_USERNAME}")

You may be able to keep -w or --no-password flags, but if you continue receiving command terminal errors regarding the command-line arguments, remove both of those flags completely as shown above and let the command terminal ask you for the password to the postgres server as it does by default.

user4992261
  • 31
  • 1
  • 9
0

I just went through this on a Windows 7 machine.

The correct answer is:

system("createdb --no-password --username=#{DB_USERNAME} #{DB_NAME} && createdb --no-password --username=#{DB_USERNAME} #{TEST_DB_NAME} ")

I got this from your posts and from reading the dbcreate --help. The arguments were in the wrong order; notice the DB_NAME is at the end as a final argument after all the options.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56