本文介绍了无法使用gem“pg”(这不是rails,只是纯ruby)从我的Ruby脚本连接到Postgres(pg)数据库,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Mac OSX计算机下载了postgres.app。我有一个rails应用程序已连接和使用Postgres DB与postgres应用程序。



现在我编写一个纯Ruby测试脚本(不是Rails,纯Ruby,甚至不是活动记录)来尝试连接到postgres数据库实例。

  1)在终端上运行此命令:
gem install pg - --with-pg-config = / Applications / Postgres.app / Contents / MacOS / bin / pg_config

2)将这个代码添加到测试脚本:

#!/ usr / bin / env ruby​​
#encoding:utf-8
requirepg


@conn = PG.connect(
:dbname =>'oData',
:user =>'am',
:password =>'')

@ conn.exec(CREATE TABLE程序(id serial NOT NULL,name character varying(255););

本 p>

当我运行这个脚本时,我得到以下错误:

  /am/.rvm/gems/ruby-2.0.0-p247/gems/pg-0.16.0/lib/pg.rb:38:in'initialize':could 
不能连接到服务器:没有这样的文件或目录(PG :: ConnectionBad)
服务器是否在本地运行,并且在Unix域套接字/var/pgsql_socket/.s.PGSQL.5432上接受
连接?

我的调试努力:

 我的postgres.app已启动并正在运行。 

我查看了pg gem [documentation] [2],我的语法似乎没问题。

我的postgres数据库的位置输入在我的bash脚本像这样
PATH =/ Applications / Postgres.app / Contents / MacOS / bin:$ PATH

不知道接下来该做什么。

解决方案

我使用活动记录gem使连接工作,很好


I downloaded the postgres.app for my Mac OSX machine. I have a rails app that has connected and used the postgres DB that came with the postgres app.

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. 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.

解决方案

I used active record gem to make the connection work and it was fine

这篇关于无法使用gem“pg”(这不是rails,只是纯ruby)从我的Ruby脚本连接到Postgres(pg)数据库,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:37