【问题标题】:Cannot migrate sqlite3 to postgres in rails using taps无法使用水龙头将 sqlite3 迁移到 rails 中的 postgres
【发布时间】:2016-06-25 22:21:23
【问题描述】:

我正在尝试将我的 rails 数据库从 sqlite3 迁移到 postgresql,以便可以推送到 heroku。我也在尝试迁移数据。
这是我的gemfile:

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5'
# Use sqlite3 as the database for Active Record

# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'
gem "twitter-bootstrap-rails"
# Use Unicorn as the app server
# gem 'unicorn'
gem 'therubyracer'
gem 'bootstrap-sass'
gem 'yaml_db'
gem 'will_paginate', '~> 3.1.0'
gem 'sinatra'
gem 'taps', '>= 0.3.22'
gem "tilt", '~> 1.4.1'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do

    gem 'byebug'
end

group :development do

    gem 'web-console', '~> 2.0'
    gem 'sqlite3'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
    gem 'spring'
end
gem "sequel_pg", :require=>'sequel'
group :production do

    gem 'rails_12factor'
    gem 'puma'

end

我还更改了我的 database.yml 文件以迁移到 postgres

development:
  adapter: postgresql
  encoding: unicode
  database: gainxp_development
  pool: 5
  username: sam
  password: pass
  host: localhost


# 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
  encoding: unicode
  database: gainxp_test
  pool: 5
  username: sam
  password: pass
  host: localhost

production:
  adapter: postgresql
  encoding: unicode
  database: gainxp_production
  pool: 5 
  username: sam
  password: pass
  host: localhost

我运行了以下命令来启动我的 Taps 服务器并且它正确启动了:

taps server sqlite://db/development.sqlite3 sam pass -p $PORT 

在我运行的另一个终端中:

sudo taps pull postgres://sam:pass@localhost/gainxp_development http://sam:pass@localhost:$PORT

这给了我这个错误:

Failed to connect to database:
  Sequel::DatabaseConnectionError -> PG::ConnectionBad: FATAL:  database "gainxp_development" does not exist

我的 pg_hba.conf 文件如下

# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
#
# Database administrative login by Unix domain socket
local   all             postgres                                trust


# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

我已经在这几周试图迁移我的数据库。感谢您的帮助

【问题讨论】:

    标签: ruby-on-rails ruby postgresql sqlite


    【解决方案1】:

    我不完全确定问题或解决方案可能是什么,但我建议您尝试以下方法:

    1. 在您的 Gemfile 中添加 gem 'pg'
    2. 运行bundle install
    3. 运行rake db:setuprake db:migrate
    4. 运行taps 命令

    【讨论】:

    • 我已经在 sequel_pg 下的 gemfile 中有 postgres,当我运行 rake db:setup 我得到 PG::InsufficientPrivilege: ERROR: permission denied to create database : CREATE DATABASE "gainxp_development" ENCODING = 'unicode'
    • 我的问题可能听起来有点傻,但是,您的 postgres 用户是否有数据库创建权限?
    • 我确定,但如果您能够使用超级角色登录您的 postgres,您可以这样做:ALTER USER sam CREATEDB; 参考:dba.stackexchange.com/questions/33285/…
    • 我这样做了,现在我得到: PG::InvalidParameterValue: 错误:新编码 (UTF8) 与模板数据库的编码 (SQL_ASCII) 不兼容 提示:使用与模板中相同的编码数据库,或使用 template0 作为模板。 :创建数据库“gainxp_development”编码='utf8'
    • 我从来不需要处理编码问题,我所拥有的只是:[适配器、池、超时、数据库、[模板]]。此外,您应该删除生产环境中的密码和用户名字段,因为 Heroku 通常会提供一个 ENV['DATABASE_URL'] 供您的应用程序使用
    猜你喜欢
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    相关资源
    最近更新 更多