简体   繁体   English

如何使用Sequel连接到postgres数据库?

[英]How do I connect to a postgres database with Sequel?

I was making a web app to deploy using Heroku.com when I realized that the only database type they support is PostgreSQL. 当我意识到他们支持的唯一数据库类型是PostgreSQL时,我正在使用Heroku.com制作一个Web应用程序进行部署。 Up until now, my app (powered by the Ruby gem Sinatra) accessed the database via the .Sqlite method for the Sequel gem. 到目前为止,我的应用程序(由Ruby gem Sinatra提供支持)通过.Seql gem的.Sqlite方法访问数据库。

Here's my Ruby script from when I used Sequel to access the .db file via SQLite: 这是我使用Sequel通过SQLite访问.db文件时的Ruby脚本:

DB = Sequel.sqlite('mydatabase.db')
DB.create_table :mytable do
  primary_key :id
  String :column_name
end


I installed PostgreSQL after learning Heroku used only that. 我在学习Heroku后才安装了PostgreSQL。 Here's the script via postgres (my username is literally 'postgress', though I obviously won't reveal my password in this question): 这是通过postgres的脚本(我的用户名字面意思是'postgress',但我显然不会在这个问题中透露我的密码):

DB = Sequel.postgres('mydatabase.db',:user=>'postgres',:password=>'my_password_here',:host=>'localhost',:port=>5432,:max_connections=>10)
DB.create_table :mytable do
  primary_key :id
  String :column_name 
end

However, when I run this code, I get the following error: 但是,当我运行此代码时,我收到以下错误:

C:/Ruby193/lib/ruby/gems/1.9.1/gems/sequel-3.38.0/lib/sequel/adapters/postgres.rb:208:in 'initialize': PG::Error: FATAL: database "mydatabase.db" does not exist (Sequel::DatabaseConnectionError)

I've tried searching Google, StackOverflow, Sequel documents, and the Heroku help documents for any help, but I've found no fix to this problem. 我已经尝试搜索Google,StackOverflow,Sequel文档和Heroku帮助文档以获得任何帮助,但我找不到解决此问题的方法。

Does anyone know what I am doing wrong? 有谁知道我做错了什么?

The database mydatabase.db doesn't exist, as per the error message from Pg. 根据Pg的错误消息,数据库mydatabase.db不存在。 Likely reasons: 可能的原因:

  • You probably meant mydatabase without the SQLite-specific .db filename suffix 您可能意味着没有SQLite特定的.db文件名后缀的mydatabase
  • It's possible you created the db with different case, eg "Mydatabase.db" ; 您可以使用不同的大小写创建数据库,例如"Mydatabase.db" ;
  • You might be connecting to a different Pg server than you think you are 您可能正在连接到不同于您认为不同的Pg服务器
  • You never created the database . 您从未创建过数据库 Unlke SQLite's default behaviour, Pg doesn't create databases when you try to connect to a database that doesn't exist yet. Unlke SQLite的默认行为,当您尝试连接到尚不存在的数据库时,Pg不会创建数据库。

If in doubt, connect to Pg with psql and run \\l to list databases, or connect via PgAdmin-III. 如果有疑问,请使用psql连接到Pg并运行\\l列出数据库,或通过PgAdmin-III连接。

The PostgreSQL documentation and tutorial are highly recommended, too. 强烈建议使用PostgreSQL 文档教程 They're well written and will teach you a lot about SQL in general as well as Pg in particular. 它们编写得很好,一般会教你很多SQL,特别是Pg。

BTW, the postgres user is a superuser. BTW, postgres用户是超级用户。 You should not be using it for your application; 您不应该将它用于您的应用程序; it's like running your server as root, ie a really bad idea. 这就像以root身份运行您的服务器,即一个非常糟糕的主意。 Create a new PostgreSQL user without superuser, createdb or createuser rights and use that for your application. 创建一个没有超级用户,createdb或createuser权限的新PostgreSQL用户,并将其用于您的应用程序。 You can either CREATE DATABASE somedb WITH OWNER myappuser - or preferably, create the database owned by a different user to your webapp user and then expicitly GRANT the webapp user the minimum required permissions. 您可以CREATE DATABASE somedb WITH OWNER myappuser -或最好,建立由不同的用户拥有对你的web应用的用户数据库,然后expicitly GRANT web应用程序的用户所需的最低权限。 See user management and GRANT . 请参阅用户管理GRANT

在heroku上,您需要做的就是告诉Sequel连接到DATABASE_URL环境变量的内容(这是Sequel理解的正确形成的URL):

DB = Sequel.connect(ENV['DATABASE_URL']) 

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

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