Ich habe SQLite in der Entwicklung und im Test verwendet undPostgreSQL in Produktion auf Heroku. Ich möchte SQLite durch PostgreSQL ersetzen. Ich programmiere in der Cloud9-Umgebung (Rails 4). Ich habe keine Daten, die ich möglicherweise verlieren könnte.
Was habe ich getan:
Zuerst habe ich die Datenbank.yml bearbeitet:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
Dann:
- Im Gemfile bin ich umgezogen
gem "pg"
von der Produktionsumgebung nur in alle Umgebungen und entferntgem "sqlite3"
- ich rannte
bundle install
. - ich rannte
sudo service postgresql start
- ich rannte
sudo sudo -u postgres psql
- Und trat ein
create database "app_development";
- Trat ein
q
Aktualisieren: Ich habe die folgenden zusätzlichen Schritte hinzugefügt:
- Ich habe einen neuen Benutzer in psql mit erstellt
CREATE USER my_username SUPERUSER PASSWORD "my_password";
- In database.yml habe ich das hinzugefügt
username
undpassword
- In database.yml habe ich hinzugefügt
host: myurl.c9.io
- Ich habe in psql eingegeben:
GRANT ALL ON DATABASE app_development to my_username
Laufen sudo sudo -u postgres psql
und dann list
produziert:
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 |
Ich sehe meinen Nutzernamen hier nicht als Besitzer von app_development ...
Error: Laufen rake db:migrate
mal raus, besagen 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?
.
Warum schlägt die Verbindung mit PostgreSQL fehl?
Antworten:
1 für die Antwort № 1Ersetzen Sie den Inhalt der Datei database.yml durch:
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
Da Sie Heroku verwenden, können Sie den Produktionsbereich so lassen, wie er ist