任何使用 Rails 5.1.2 遇到此问题的人(一年后!)我在第一次安装和启动 Postgresql (Centos 7) 后做了以下操作。
因此,假设您已经安装了 postgresql 并设置了 postgres 服务器和用户。(+ Linux 常用的标准开发工具)。
Add extra deps for rails to build gems.
$ sudo yum install postgresql-devel
Add postgres path in ~/.profile
export PATH=/usr/bin/postgres:$PATH (or your installed path)
Add another user/role with create db privileges using pgAdmin or shell
(should be the same user as the system/rails user because the postgres user doesn't have permissions for /rails/db/schema.rb, but the system/rails user does)
Below are shell commands for postgres create role and database.
$ sudo -u postgres psql (enter postgres password)
$ create role (linux/rails user) with createdb login password 'password';
$ \du (check its done and has createDB privs)
$ CREATE DATABASE name;
如果没有其他参数,您将自动成为新数据库的所有者。
或者您也可以使用像 DBeaver 这样的 gui 来做同样的事情。
So the above sets up for rails to access postgresql and build the pg gem once you've swapped out the default Gemfile & config/database.yml
Now create app and set it up (no need for -d postgresql flag because we swap out the Gemfile and the config/database.yml file contents completely and rails will install a postgresql db on bundle update/install.
(change some to rake for earlier versions of rails)
$ rails new app
$ cd app
$ atom (or editor) Gemfile config/database.yml
Swap out both file contents (to ones shown below) & save.
$ bundle update
$ bundle install
Check it with
$ rails db:create
(postgresql database should now be connected), so scaffold something
$ rails g scaffold Users name:string email:string comment:text
$ rails db:migrate
$ rails server
http://localhost:3000 shows the default page and http://localhost:3000/users brings up your new Users page using postgresql not sqlite3. Put something in to test it.
下面是我用于 Rails 5.1.2 的 Gemfile 和 config/database.yml 文件,包括 Heroku 的 tap。
Gemfile 轨道 5.1.2
source 'https://rubygems.org'
gem 'rails', '5.1.2'
gem 'puma', '3.9.1'
gem 'sass-rails', '5.0.6'
gem 'uglifier', '3.2.0'
gem 'coffee-rails', '4.2.2'
gem 'jquery-rails', '4.3.1'
gem 'turbolinks', '5.0.1'
gem 'jbuilder', '2.7.0'
gem 'taps'
#Postgresql Database
group :production do
gem 'pg', '0.21.0'
end
group :development, :test do
gem 'sqlite3', '1.3.13'
gem 'byebug', '9.0.6', platform: :mri
end
group :development do
gem 'web-console', '3.5.1'
gem 'listen', '3.0.8'
gem 'spring', '2.0.2'
gem 'spring-watcher-listen', '2.0.1'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
config/database.yml file contents (watch out for indentation)
development:
adapter: postgresql
encoding: unicode
database: development or app_name
pool: 5
username: (user created for postgres/rails)
password: password
host: localhost
test:
adapter: postgresql
encoding: unicode
database: development or app_name
pool: 5
username: (user created for postgres/rails)
password: password
host: localhost
production:
adapter: postgresql
encoding: unicode
database: development or app_name
pool: 5
username: (user created for postgres/rails)
password: password
host: localhost
执行上述所有操作将为您在 rails 中获得一个 dev/prod postgresql 数据库,但无需测试,您也可以通过运行“rails db”并输入密码直接从 rails 访问数据库。
它很简单,但可以让您快速启动并运行 rails/postgresql。