簡體   English   中英

無法使用寶石“ pg”從我的Ruby Script連接到Postgres(pg)數據庫(這不是rails,僅僅是純紅寶石)

[英]cannot connect to Postgres (pg) database from my Ruby Script using gem “pg”(This is not rails, just pure ruby)

我為Mac OSX計算機下載了postgres.app。 我有一個已連接並使用了postgres應用程序隨附的postgres DB的Rails應用程序。

現在,我正在編寫一個純Ruby測試腳本(不是Rails,純Ruby,甚至不是Active Record)來嘗試連接到postgres數據庫實例。 這些是我按照此步驟進行設置的步驟

 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);");

我是從這個鏈接得到的

當我運行此腳本時,出現以下錯誤:

   /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 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"

不知道下一步該怎么做。 任何幫助,將不勝感激,謝謝。

您確定postgres正在偵聽套接字嗎? 您確定用戶名和密碼正確嗎?

我傾向於嘗試類似的東西

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)

我使用活動記錄寶石來使連接正常工作,這很好

連接設置對我有效。

但是那個代碼應該看起來像

@ conn.exec(“創建表程序(id串行NOT NULL,名稱字符(255))”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM