我在開發和測試中使用SQLite,而在Heroku上使用PostgreSQL。我想用PostgreSQL替換SQLite。我在Cloud9環境中編程(Rails 4)。我沒有可能會丟失的數據。試圖從SQLite切換到PostgreSQL
我做了什麼:
首先,我編輯的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
更新:我增加了以下額外的步驟:
- 我創建了一個PSQL新用戶
CREATE USER my_username SUPERUSER PASSWORD 'my_password';
- 在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中提到的用戶名/密碼。 ... –
我應該如何設置密碼?我輸入了'sudo sudo -u postgres psql',然後輸入'\ password',然後輸入我的新密碼。在database.yml中,我明白我應該包含'username:<%= ENV ['USERNAME']%>','password:<%= ENV ['PASSWORD']%>'和host:<%= ENV [ IP']%>'。然後我應該在什麼文件中輸入密碼(可能在config \ secrets.yml中)?另外兩個變量應該是什麼? – Nick