[英]cannot connect to Postgres (pg) database from my Ruby Script using gem “pg”(This is not rails, just pure ruby)
I downloaded the postgres.app for my Mac OSX machine. 我为Mac OSX计算机下载了postgres.app。 I have a rails app that has connected and used the postgres DB that came with the postgres app.
我有一个已连接并使用了postgres应用程序随附的postgres DB的Rails应用程序。
Now I am writing a pure Ruby test script (Not Rails, pure Ruby, not even Active Record) to try to connect to the postgres database instance. 现在,我正在编写一个纯Ruby测试脚本(不是Rails,纯Ruby,甚至不是Active Record)来尝试连接到postgres数据库实例。 These are the steps I followed to set this up
这些是我按照此步骤进行设置的步骤
1) ran this command on the terminal:
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
2) Added this code to the test script:
#!/usr/bin/env ruby
#encoding: utf-8
require "pg"
@conn = PG.connect(
:dbname => 'oData',
:user => 'am',
:password => '')
@conn.exec("CREATE TABLE programs (id serial NOT NULL, name character varying(255);");
I got this from this link 我是从这个链接得到的
When I ran this script I get the following error: 当我运行此脚本时,出现以下错误:
/Users/am/.rvm/gems/ruby-2.0.0-p247/gems/pg-0.16.0/lib/pg.rb:38:in `initialize': could
not connect to server: No such file or directory (PG::ConnectionBad)
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
My Debug efforts: 我的调试工作:
My postgres.app is up and running.
I looked at the pg gem [documentation][2] and my syntax seemed OK.
The location of my postgres DB is entered in my bash script like so
PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"
Not sure what to do next. 不知道下一步该怎么做。 Any help would be appreciated, Thanks.
任何帮助,将不胜感激,谢谢。
are you sure postgres is listening on a socket? 您确定postgres正在侦听套接字吗? are you sure the username and password is right?
您确定用户名和密码正确吗?
I would be inclined to try something like 我倾向于尝试类似的东西
require 'pg'
puts PG::Connection.ping(:dbname => 'oData',:user => 'am',:password => '')
puts "trying with tcp"
puts PG::Connection.ping(:dbname => 'oData',:user => 'am',:password => '', :port => 5432)
我使用活动记录宝石来使连接正常工作,这很好
Settings to connect works for me. 连接设置对我有效。
But that code should look like 但是那个代码应该看起来像
@conn.exec("CREATE TABLE programs (id serial NOT NULL, name character(255))")
@ conn.exec(“创建表程序(id串行NOT NULL,名称字符(255))”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.