简体   繁体   English

createdb:命令行参数过多

[英]createdb: too many command line arguments

I'm a newbie to Ruby and web development. 我是Ruby和Web开发的新手。 I'm using Windows 7 64-bit with Ruby 2.0 and I have PostgreSQL 9.4 installed. 我将Windows 7 64位和Ruby 2.0一起使用,并且安装了PostgreSQL 9.4。

I'm trying to use ActiveRecord to create a database. 我正在尝试使用ActiveRecord创建数据库。 I checked that my postgresql server is running and I did bundle install to make sure I had all the required gems. 我检查我的postgresql服务器是否正在运行,并进行捆绑安装以确保拥有所有必需的gem。 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: 当我运行“ bundle exec rake db:create”时,rake似乎能够在PostgreSQL目录中找到createdb.exe,但是由于某种原因,它告诉我我有太多命令行参数:

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 我的Gemfile,Rakefile和config.rb文件在我以前的文章中: 使用ActiveRecord时createdb无法识别为命令

I think the issue might be in the Rakefile here: 我认为问题可能出在以下Rakefile中:

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. 我尝试删除和更改-w的顺序,如下所示: pg_dump:命令行参数过多 ,但这并不能解决问题。

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://grokbase.com/t/postgresql/pgsql-general/07avnzajn7/createdb-argument-question

http://postgresql.nabble.com/pgpass-does-not-work-for-createlang-td2118667.html 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: 由于您使用的是Windows,因此该行的语法可能存在问题:

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. 如果Ruby使用默认的Windows cmd.exe运行命令,则&&运算符将不起作用。

I would probably break it up into 2 statements: 我可能将其分解为2条语句:

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. 最后,如果问题仍然存在,则PATH中必须有另一个createdb可执行文件,该可执行文件在Postgres二进制文件路径之前被单击。 You could try system("path") and see what the output is, and check those dirs for a conflicting createdb . 您可以尝试system("path")看看输出是什么,然后检查那些目录中是否有冲突的createdb

The Rakefile "create" task uses createdb.exe from PostgreSQL. Rakefile的“创建”任务使用PostgreSQL中的createdb.exe。 system() takes in options for createdb.exe. system()接受了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. 对于此特定的Rakefile,选项标志的格式错误,因此createdb.exe无法理解为其提供的命令行参数。 Specifically, single option flags and double option flags are being used together incorrectly: 特别是,单选项标志和双选项标志一起使用不正确:

  • --username must be set equal to a value --username必须设置为等于一个值
  • -w and --no-password mean the same thing and are redundant. -w和--no-password含义相同,并且是多余的。 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: 此Rakefile中system()的正确语法是:

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. 您也许可以保留-w或--no-password标志,但是如果您继续收到有关命令行参数的命令终端错误,请完全如上所示删除这两个标志,并让命令终端询问您密码默认情况下连接到postgres服务器。

I just went through this on a Windows 7 machine. 我只是在Windows 7计算机上进行了此操作。

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 . 我是从您的帖子以及阅读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. 请注意, 所有选项之后,最后一个参数是DB_NAME

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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