First of all you need to install PostgreSQL:
sudo zypper install postgresql92-server postgresql92-develThis installs the latest available version. The devel package is needed to build the Rails drivers.
Then start the database server:
sudo /usr/sbin/rcpostgresql startIf you want to automatically start the server on boot enable the service with:
sudo chkconfig -a postgresqlTo finalize the setup of the database server you need to create a user, which Rails uses to access the database. You can choose whatever username you like, but the setup is most simple, if you use the username of the user under which you do your Rails development. The -d option allows the user to create databases, so the corresponding rake tasks can do their job.
sudo sudo -u postgres createuser -d USERNAME
sudo sudo -u postgres psql
By querying the users table you can check the existing users and their rights on the PostgreSQL console:
SELECT * FROM pg_user;
To connect Rails to the database, you need to create the database configuration in Rails. Create a file config/database.yml with something like the following content. You need to replace USERNAMEbundle19 install
development:
adapter: postgresql
database: APPNAME_development
username: USERNAME
test:
adapter: postgresql
database: APPNAME_test
username: USERNAME
production:
adapter: postgresql
database: APPNAME_production
username: USERNAMEThen finally you need to create the databases with:
rake db:createand create the database tables by running the migrations of your Rails app:
rake db:migrateThat's all. Now your Rails app runs on PostgreSQL, and you can do all your development and testing as usual.
Instead of using using sudo everytime, one could also switch to postres user, e.g. by "su -l postgres".
ReplyDeleteTo create the db from scratch the migrations should not be used, but
rake db:create
rake db:setup
Above does not create the production DB, this is done by
rake db:create:all
(http://guides.rubyonrails.org/v3.1.1/migrations.html#what-are-schema-files-for)