Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7938929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:56:40+00:00 2026-06-03T22:56:40+00:00

I’m failing at finding out how to create a database from ActiveRecord if one

  • 0

I’m failing at finding out how to create a database from ActiveRecord if one doesn’t already exist. I’ve looked at this: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html#method-i-create_database, but it doesn’t want to play nice, as far as I can tell… I’m not sure what I’m missing here. Is this even possible?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:56:41+00:00Added an answer on June 3, 2026 at 10:56 pm

    I managed to cobble together this thing that works with both heroku and on my local machine, and will create a database if one doesn’t exist.

    # ./lib/db_connect.rb
    
    require 'erb'
    require 'uri'
    require 'em-synchrony/activerecord'
    
    class DbConnect
      attr_accessor :config
      def initialize
        @db = URI.parse(ENV['DATABASE_URL'] || 'http://localhost')
        if @db.scheme == 'postgres' # This section makes Heroku work
          ActiveRecord::Base.establish_connection(
            :adapter  => @db.scheme == 'postgres' ? 'postgresql' : @db.scheme,
            :host     => @db.host,
            :username => @db.user,
            :password => @db.password,
            :database => @db.path[1..-1],
            :encoding => 'utf8'
          )
        else # And this is for my local environment
          environment = ENV['DATABASE_URL'] ? 'production' : 'development'
          @db = YAML.load(ERB.new(File.read('config/database.yml')).result)[environment]
          ActiveRecord::Base.establish_connection(@db)
          @config = ActiveRecord::Base.connection.pool.spec.config
        end
      end
    end
    
    # connect to the database
    DbConnect.new
    

    And here’s a Rakefile that uses it.

    # ./Rakefile
    
    $: << './lib'
    require 'db_connect'
    
    db = DbConnect.new
    config = db.config
    
    desc 'create the database'
    task :create_db do
      unless ENV['DATABASE_URL'].present?
        ActiveRecord::Base.establish_connection adapter:'postgresql',username:config[:username],password:config[:password], database: 'postgres'
        ActiveRecord::Base.connection.create_database config[:database]
      else
        raise StandardError, "You cannot do this on Heroku!"
      end
    end
    
    desc 'create the users table'
    task :create_users_table do
      ActiveRecord::Schema.define(:version => 0) do
        create_table "users", :force => true do |t|
          t.string   "first_name"
          t.string   "last_name"
          t.datetime "created_at", :null => false
          t.datetime "updated_at", :null => false
        end
      end
    end
    

    Problem solved.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.