This blog describes how you can create a Rails application accessing PostgreSQL database using NetBeans 6.
- Download and Configure PostgreSQL
- Download PostgreSQL from here.
- After download and install, open the PostgreSQL command prompt (from the program menu) and initialize the database by giving the following command (in
bin
directory):initdb -D "\users\Arun Gupta\postgresql\data"
Note, the directory specified in the command does not exist and will be created after the command is executed. The following output is shown:
The files belonging to this database system will be owned by user "Arun Gupta".
This user must also own the server process.The database cluster will be initialized with locale English_United States.1252.
fixing permissions on existing directory /users/Arun Gupta/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers/max_fsm_pages ... 32MB/204800
creating configuration files ... ok
creating template1 database in /users/Arun Gupta/postgresql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... okWARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.Success. You can now start the database server using:
"postgres" -D "/users/Arun Gupta/postgresql/data"
or
"pg_ctl" -D "/users/Arun Gupta/postgresql/data" -l logfile startPlease note the username shown in the first line of command output ("Arun Gupta" in this case). This will be required later for configuring
database.yml.
- Start the PostgreSQL by giving the following command:
"postgres" -D "/users/Arun Gupta/postgresql/data"
The following output is shown:
LOG: database system was shut down at 2008-01-10 22:11:01
LOG: checkpoint record is at 0/4872F8
LOG: redo record is at 0/4872F8; undo record is at 0/0; shutdown TRUE
LOG: next transaction ID: 0/593; next OID: 10820
LOG: next MultiXactId: 1; next MultiXactOffset: 0
LOG: database system is ready
- Create a JRuby-on-Rails project using PostgreSQL
- Using NetBeans, create a Rails project. Select PostgreSQL as the database as shown below:
- Update the Development database in "
database.yml
" to match:development:
adapter: postgresql
host: localhost
port: 5432
database: RailsApplication1_Development
username: Arun Gupta
password:The default port and username are used.
- Install a pure-Ruby Postgres database binding by giving the following commands:
C:\Program Files\NetBeans 6.0\ruby1\jruby-1.0.2\bin>jruby gem install postgres-pr
Bulk updating Gem source index for: http://gems.rubyforge.org
Successfully installed postgres-pr-0.4.0Adding the gem using "
Tools
" -> "Ruby Gems
" encounters the issue #122593.
- Do the migrations
- Open PostgreSQL command prompt and create the database by giving the following command (in
bin
directory):createdb RailsApplication1_Development
Note this is case sensitive. The following output is shown:
CREATE DATABASE
- Create a new model by right-clicking on the project, selecting "
Generate
", "model
" from the list box and giving the name as "wish
". - Expand "
Database Migrations
", "migrate
" and open "001_create_wishes.rb
". Change "self.up
" helper method as shown below:def self.up
create_table :wishes do |t|
t.column :greeting, :string
end
Wish.create :greeting => "Hello PostgreSQL!"
endThis instructs the Rails framework to a new table and populate it with three rows upon migration.
- Invoke
db:migrate
by right-clicking on the project, selecting "Run Rake Task
", "db
" and then "migrate
". This creates the appropriate tables and populate the it with three rows as mentioned above.
- Open PostgreSQL command prompt and create the database by giving the following command (in
- Add Controller & View
- Right-click on project, select "
Generate...
". - Take the default value in list box, which is "
controller
". Specify the value of "Name:
" as "show
" and value of "Views:
" as "wishes
". - Expand "
Controllers
" and open "show_controller.rb
" and update the "wishes
" helper method as shown below:def wishes
@wish = Wish.find(1).greeting;
end - Expand "
Views
", "show
", "wishes.rhtml
" and add the following fragment as the last line:<%= @wish %>
- Right-click on project, select "
- Run the project
- Open "
wishes.rhtml
" and hitShift+F6
(default keystroke to run the file). The output is shown as:
- Open "
Please leave suggestions on other TOTD that you’d like to see. A complete archive is available here.
Technorati: totd netbeans ruby jruby postgresql windows
Related posts:- TOTD #9: Using JDBC connection pool/JNDI name from GlassFish in Rails Application
- JRuby on Rails, NetBeans 6 and GlassFish V2 – Simplified Steps
- TOTD #107: Connect to Oracle database using NetBeans
- TOTD #108: Java EE 6 web application (JSF 2.0 + JPA 2.0 + EJB 3.1) using Oracle, NetBeans, and GlassFish
- TOTD #24: Getting Started with Rails 2.0.x in JRuby 1.0.3 and JRuby 1.1RC1