Connecting to postgres


#1

Hello everyone!

Please forgive my ignorance, am pretty new to rails.

Am creating an application on rails framework that will use a legacy
postgres
database with about 119 tables and 121 sequences that define the primary
key fields.
The database is ready now and i need to hook it to active:record:Base
i have installed these two gems:
postgres-pr
ruby-postgres

My YML file reads like this:

development:
adapter: postgresql
database: DB_NAME
username: george
password: tanzanite
host: localhost
schema_order: public

Warning: The database defined as ‘test’ will be erased and

re-generated from your development database when you run ‘rake’.

Do not set this db to the same as development or production.

test:
adapter: postgresql
database: DB_NAME_test
username: tanzanite
password: password
host: localhost
schema_order: public

production:
adapter: postgresql
database: DB_NAME_production
username: tanzanite
password: password
host: localhost
schema_order: public

i then wrote a configuration file that stops pluralization of the
tables.
i generated a model scaffold to “test the waters” and lol and behold! it
could not connect to through active:record.

Please can someone point me to a useful resource to guide me on how to
accomplish this. All resources seem to use mysql and am only finding no
information on connecting to postgres legacy databases.

How do i map the recquired id field to my user-defined primary key
generically named Tablename_id?

What configuration details am i missing?

Thank you.

rgds
tanzanite


#2

Hello,

database.yml seems to be ok (maybe schema_order… i don’t know i
don’t use this parameter).
are you sure about your postgresql database config?
can you connect from application server to your postgresql database?
if not check :

  • pg_hba.conf should be configure to allow application server to
    connect.
  • and listen_addresses in postgresql.conf should be correct to.

bye

On 28 fév, 02:24, Tanzanite T. <rails-mailing-l…@andreas-


#3
  1. What exceptions are in logs?

  2. For legacy databases you might find useful class methods
    set_primary_key, set_table_name, set_sequence_name, see
    http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001013
    and primary_key_prefix_type
    http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/base.rb?rev=6139#L279

2007/2/28, Tanzanite T. removed_email_address@domain.invalid:


#4

Lugovoi N. wrote:

  1. What exceptions are in logs?

  2. For legacy databases you might find useful class methods
    set_primary_key, set_table_name, set_sequence_name, see
    http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001013
    and primary_key_prefix_type
    http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/base.rb?rev=6139#L279

2007/2/28, Tanzanite T. removed_email_address@domain.invalid:

Comments appreciated

This is what is in the logs

ActiveRecord::StatementInvalid (RuntimeError: ERROR C42501 Mpermission
denied for relation feature Faclchk.c L1395 Raclcheck_error: SELECT
count(*) AS count_all FROM feature ):
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/connection_adapters/abstract_adapter.rb:128:in
log' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/connection_adapters/postgresql_adapter.rb:152:inexecute’
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/connection_adapters/postgresql_adapter.rb:433:in
select' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/connection_adapters/abstract/database_statements.rb:13:inselect_one’
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/connection_adapters/abstract/database_statements.rb:19:in
select_value' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/calculations.rb:212:inexecute_simple_calculation’
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/calculations.rb:121:in
calculate' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/calculations.rb:117:incatch’
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/calculations.rb:117:in
calculate' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.1/lib/active_record/calculations.rb:45:incount’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/pagination.rb:173:in
count_collection_for_pagination' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/pagination.rb:197:inpaginator_and_collection_for’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/pagination.rb:130:in
paginate' /app/controllers/admin_controller.rb:12:inlist’
/app/controllers/admin_controller.rb:3:in index' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/base.rb:1095:insend’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/base.rb:1095:in
perform_action_without_filters' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/filters.rb:632:incall_filter’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/filters.rb:634:in
call_filter' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/filters.rb:619:inperform_action_without_benchmark’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/benchmarking.rb:66:in
perform_action_without_rescue' C:/ruby/lib/ruby/1.8/benchmark.rb:293:inmeasure’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/benchmarking.rb:66:in
perform_action_without_rescue' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/rescue.rb:83:inperform_action’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/base.rb:430:in
send' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/base.rb:430:inprocess_without_filters’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/filters.rb:624:in
process_without_session_management_support' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/session_management.rb:114:inprocess’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.1/lib/action_controller/base.rb:330:in
process' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/dispatcher.rb:41:indispatch’
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/webrick_server.rb:113:in
handle_dispatch' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/webrick_server.rb:79:inservice’
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in service' C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
C:/ruby/lib/ruby/1.8/webrick/server.rb:173:in start_thread' C:/ruby/lib/ruby/1.8/webrick/server.rb:162:instart’
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in start_thread' C:/ruby/lib/ruby/1.8/webrick/server.rb:95:instart’
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in each' C:/ruby/lib/ruby/1.8/webrick/server.rb:92:instart’
C:/ruby/lib/ruby/1.8/webrick/server.rb:23:in start' C:/ruby/lib/ruby/1.8/webrick/server.rb:82:instart’
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/webrick_server.rb:63:in
dispatch' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/commands/servers/webrick.rb:59 C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
require' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/active_support/dependencies.rb:496:inrequire’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/active_support/dependencies.rb:343:in
new_constants_in' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.0/lib/active_support/dependencies.rb:496:inrequire’
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.1/lib/commands/server.rb:39
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
./script/server:3
-e:4:in `load’
-e:4


#5

2007/2/28, Tanzanite T. removed_email_address@domain.invalid:

This is what is in the logs

ActiveRecord::StatementInvalid (RuntimeError: ERROR C42501 Mpermission
denied for relation feature Faclchk.c L1395 Raclcheck_error: SELECT
count(*) AS count_all FROM feature ):

So, the problem shows out clear: you connect to database with postgres
role that does not have enough privilieges.
Make sure you did GRANT’ed necessary access to database objects.
http://www.postgresql.org/docs/current/static/sql-grant.html


#6

Lugovoi N. wrote:

2007/2/28, Tanzanite T. removed_email_address@domain.invalid:

This is what is in the logs

ActiveRecord::StatementInvalid (RuntimeError: ERROR C42501 Mpermission
denied for relation feature Faclchk.c L1395 Raclcheck_error: SELECT
count(*) AS count_all FROM feature ):

So, the problem shows out clear: you connect to database with postgres
role that does not have enough privilieges.
Make sure you did GRANT’ed necessary access to database objects.
http://www.postgresql.org/docs/current/static/sql-grant.html

Hey!

Thank you!
Thank you!

it worked

These are the additional configurations i have added:
i have manually set the primary key to the actual name of the primary
key in my table. This is not a DRY compliant method and i have to change
that so that i dont have to repeat that for each of the 119 tables!

but otherwise it means it can work and for that am greatful for your
comments!

rgds
tanzanite


#7

Jean-sébastien Jney wrote:

Hello,

database.yml seems to be ok (maybe schema_order… i don’t know i
don’t use this parameter).
are you sure about your postgresql database config?
can you connect from application server to your postgresql database?
if not check :

  • pg_hba.conf should be configure to allow application server to
    connect.
  • and listen_addresses in postgresql.conf should be correct to.

bye

On 28 f�v, 02:24, Tanzanite T. <rails-mailing-l…@andreas-

Thanks i have configured connections to accept localhost
am now getting this

RuntimeError: ERROR C42501 Mpermission denied for relation feature
Faclchk.c L1395 Raclcheck_error: SELECT count(*) AS count_all FROM
feature


#8

2007/2/28, Tanzanite T. removed_email_address@domain.invalid:

These are the additional configurations i have added:
i have manually set the primary key to the actual name of the primary
key in my table. This is not a DRY compliant method and i have to change
that so that i dont have to repeat that for each of the 119 tables!

Well, using legacy database schema that does not conform to Rails
conventions and cannot be changed, is indeed not very DRY.

e.g. when defining associations one may need to declare foreign key
names and class names explicitly, but from my own experience it’s not
that bad even with dozens of tables :slight_smile:

But, you can dig postgres system tables (I mean with some ruby/sql
script) and build first scratch version of ActiveRecord models
automatically.


#9

Hi,
for all those who may be wishing to integrate postgresql into their
rails
framework, I received this from the postgresql list:

=============================================================
Given the recent discussions of applications stacks, PHP & Ruby etc. it
seems an ideal time for me to introduce a project I’ve been working on.

StackBuilder is an extension of the Windows installer for PostgreSQL
that will allow the user to quickly and easily download and install
additional software to build the application stack they desire around
PostgreSQL. The project includes the StackBuilder wizard as well as a
toolkit for building application installers.

Whilst the StackBuilder itself is still in development, the first
application installer, pgRails, is available for manual download and
testing from http://pgfoundry.org/projects/stackbuilder/

pgRails is a distribution of Ruby, Rails, and the Ruby PostgreSQL
connector all preconfigured for use with PostgreSQL on Windows 2000 and
above.

I’d like to invite anyone who is interested to download and try it out,
and report any issues or problems using the project’s trackers on
pgFoundry.

Regards, Dave.

Hope that helps someone… :wink:


Regards

Andrew