简体   繁体   中英

createdb: too many command line arguments

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.

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM