尝试从 SQLite 切换到 PostgreSQL
Trying to switch from SQLite to PostgreSQL
我在 Heroku 上的开发和测试中使用 SQLite,在生产中使用 PostgreSQL。我想用 PostgreSQL 替换 SQLite。我在 Cloud9 环境中编程 (Rails 4)。我没有可能丢失的数据。
我做了什么:
首先,我编辑了 database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
然后:
- 在 Gemfile 中,我将
gem 'pg'
从生产环境仅移动到所有环境并删除了 gem 'sqlite3'
- 我运行
bundle install
.
- 我运行
sudo service postgresql start
- 我运行
sudo sudo -u postgres psql
- 并输入
create database "app_development";
- 进入
\q
更新: 我添加了以下附加步骤:
- 我使用
CREATE USER my_username SUPERUSER PASSWORD 'my_password';
在 psql 中创建了一个新用户
- 在 database.yml 中我添加了
username
和 password
- 在database.yml中我添加了
host: myurl.c9.io
- 我在psql中输入:
GRANT ALL ON DATABASE app_development to my_username
运行 sudo sudo -u postgres psql
然后 \list
产生:
Name | Owner | Encoding | Collate | Ctype | Access privileges
postgres | postgres | SQL_ASCII | C | C |
template0 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
app_development | postgres | SQL_ASCII | C | C |
我在这里没有看到我作为 app_development...
所有者的用户名
错误: 运行 rake db:migrate
超时,显示 PG::ConnectionBad: could not connect to server: Connection timed out Is the server running on host "myurl.c9.io" (ip address) and accepting TCP/IP connections on port 5432?
.
为什么与 PostgreSQL 的连接可能会失败?
将 database.yml 文件的内容替换为:
default: &default
adapter: postgresql
host: localhost
username: yourusername
password: yourpassword
timeout: 5000
port: 5432
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
因为您使用的是 Heroku,所以您可以保留生产部分原样
我在 Heroku 上的开发和测试中使用 SQLite,在生产中使用 PostgreSQL。我想用 PostgreSQL 替换 SQLite。我在 Cloud9 环境中编程 (Rails 4)。我没有可能丢失的数据。
我做了什么:
首先,我编辑了 database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
然后:
- 在 Gemfile 中,我将
gem 'pg'
从生产环境仅移动到所有环境并删除了gem 'sqlite3'
- 我运行
bundle install
. - 我运行
sudo service postgresql start
- 我运行
sudo sudo -u postgres psql
- 并输入
create database "app_development";
- 进入
\q
更新: 我添加了以下附加步骤:
- 我使用
CREATE USER my_username SUPERUSER PASSWORD 'my_password';
在 psql 中创建了一个新用户
- 在 database.yml 中我添加了
username
和password
- 在database.yml中我添加了
host: myurl.c9.io
- 我在psql中输入:
GRANT ALL ON DATABASE app_development to my_username
运行 sudo sudo -u postgres psql
然后 \list
产生:
Name | Owner | Encoding | Collate | Ctype | Access privileges
postgres | postgres | SQL_ASCII | C | C |
template0 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | SQL_ASCII | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
app_development | postgres | SQL_ASCII | C | C |
我在这里没有看到我作为 app_development...
所有者的用户名错误: 运行 rake db:migrate
超时,显示 PG::ConnectionBad: could not connect to server: Connection timed out Is the server running on host "myurl.c9.io" (ip address) and accepting TCP/IP connections on port 5432?
.
为什么与 PostgreSQL 的连接可能会失败?
将 database.yml 文件的内容替换为:
default: &default
adapter: postgresql
host: localhost
username: yourusername
password: yourpassword
timeout: 5000
port: 5432
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
因为您使用的是 Heroku,所以您可以保留生产部分原样