TOTD #25: Rails application with PostgreSQL database using NetBeans

how-to
Feb 7, 20085 mins

This blog describes how you can create a Rails application accessing PostgreSQL database using NetBeans 6.

  1. Download and Configure PostgreSQL
    1. Download PostgreSQL from here.
    2. 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 "usersArun Guptapostgresqldata"

      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".<br>This user must also own the server process.<br><br>The database cluster will be initialized with locale English_United 
      	States.1252.<br><br>fixing permissions on existing directory /users/Arun Gupta/postgresql/data 
      	... ok<br>creating subdirectories ... ok<br>selecting default max_connections ... 100<br>selecting default shared_buffers/max_fsm_pages ... 32MB/204800<br>creating configuration files ... ok<br>creating template1 database in /users/Arun Gupta/postgresql/data/base/1 ... 
      	ok<br>initializing pg_authid ... ok<br>initializing dependencies ... ok<br>creating system views ... ok<br>loading system objects' descriptions ... ok<br>creating conversions ... ok<br>setting privileges on built-in objects ... ok<br>creating information schema ... ok<br>vacuuming database template1 ... ok<br>copying template1 to template0 ... ok<br>copying template1 to postgres ... ok<br>
      	<br>WARNING: enabling "trust" authentication for local connections<br>You can change this by editing pg_hba.conf or using the -A option the<br>next time you run initdb.<br>
      	<br>Success. You can now start the database server using:<br><br>"postgres" -D "/users/Arun Gupta/postgresql/data"<br>or<br>"pg_ctl" -D "/users/Arun Gupta/postgresql/data" -l logfile start<br>
      	<br>

      Please 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.

    3. 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<br>LOG: checkpoint record is at 0/4872F8<br>LOG: redo record is at 0/4872F8; undo record is at 0/0; shutdown TRUE<br>LOG: next transaction ID: 0/593; next OID: 10820<br>LOG: next MultiXactId: 1; next MultiXactOffset: 0<br>LOG: database system is ready

  2. Create a JRuby-on-Rails project using PostgreSQL
    1. Using NetBeans, create a Rails project. Select PostgreSQL as the database as shown below:

    2. Update the Development database in “database.yml” to match:

      development:<br>  adapter: postgresql<br>  host: localhost<br>  port: 5432<br>  database: RailsApplication1_Development<br>  username: Arun Gupta<br>  password:

      The default port and username are used.

    3. Install a pure-Ruby Postgres database binding by giving the following commands:

      C:Program FilesNetBeans 6.0ruby1jruby-1.0.2bin>jruby gem install 
      	postgres-pr<br>
      	Bulk updating Gem source index for: <a href="https://gems.rubyforge.org">https://gems.rubyforge.org</a><br>
      	Successfully installed postgres-pr-0.4.0

      Adding the gem using “

      Tools

      ” -> “

      Ruby Gems

      ” encounters the

      issue #122593

      .

  3. Do the migrations
    1. 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

    2. Create a new model by right-clicking on the project, selecting “Generate“, “model” from the list box and giving the name as “wish“.
    3. Expand “Database Migrations“, “migrate” and open “001_create_wishes.rb“. Change “self.up” helper method as shown below:

      def self.up<br>
        create_table :wishes do |t|<br>
          t.column :greeting, :string<br>
        end<br>
        Wish.create :greeting => "Hello PostgreSQL!"<br>
      	end

      This instructs the Rails framework to a new table and populate it with three rows upon migration.

    4. 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.
  4. Add Controller & View
    1. Right-click on project, select “Generate...“.
    2. Take the default value in list box, which is “controller“. Specify the value of “Name:” as “show” and value of “Views:” as “wishes“.
    3. Expand “Controllers” and open “show_controller.rb” and update the “wishes” helper method as shown below:

      def wishes<br>
        @wish = Wish.find(1).greeting;<br>
      	end
    4. Expand “Views“, “show“, “wishes.rhtml” and add the following fragment as the last line:

      <%= @wish %>

  5. Run the project
    1. Open “wishes.rhtml” and hit Shift+F6 (default keystroke to run the file). The output is shown as:

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