Search code examples
ruby-on-railsruby-on-rails-3postgresqlimportrails-postgresql

Import data from text file into PostgreSQL. "Postgres_Copy" Gem


I am using PostgreSQL with ruby and rails app and have Postgres_copy gem installed https://github.com/diogob/postgres-copy

I have hotels table and want to import text file using function pg_copy_from the gem. Data in the file is separated by the "|" symbol.

So I have created rake task

namespace :db do
  namespace :import do
    desc "Copy hotels records to the database"
    task :hotels => :environment do
      Hotel.pg_copy_from 'db/ActivePropertyList.txt', :delimiter => '|', :map => {
        'HotelID' => 'ean_hotel_id',
        'SequenceNumber' => 'sequence_number',
        'Name' => 'name',
        'Address' => 'address'}
    end
  end
end

If i run rake db:import:hotels nothing happens. What could be the problem?

UPDATE

I noticed that when I run psql and try to see all realtions by \d command, it says "No relations found."

However, I ran rake:db:create:all and rake db:migrate commands and can see the schema file. Also, \l command in psql console shows my database.

Maybe it would be easier just to run

c = Hotel.connection.raw_connection
c.exec(%q{\copy hotels (hotel_id, name) FROM 'db/ActivePropertyList.txt' DELIMITER '|'})

, but I suggest nothing happens because of the missing relations.


Solution

  • My import failed because of the upper-case issue that is common for the PostgreSQL. Renaming all header fields and database column names to lower case did the trick.