class Heroku::Command::Pg

manage heroku postgresql databases

Public Instance Methods

credentials() click to toggle source

pg:credentials [DATABASE]

Display the DATABASE credentials.

--reset       # Reset credentials on the specified database.
# File lib/heroku/command/pg.rb, line 159
def credentials
  deprecate_dash_dash_db("pg:ingress")
  reset = extract_option("--reset", false)
  case reset
  when true
    db = resolve_db(:required => 'pg:reset')
    case db[:name]
    when "SHARED_DATABASE"
      output_with_bang "Resetting password not currently supported for #{db[:pretty_name]}"
    when %r\A#{Resolver.postgres_addon_prefix}\w+/
      working_display 'Resetting' do
        return unless confirm_command
        output_with_arrow("Resetting password for #{db[:pretty_name]}")
        response = heroku_postgres_addon_client(db[:url]).reset_password
        heroku.add_config_vars(app, {"DATABASE_URL" => response["url"]}) if db[:default]
      end
    when %r\A#{Resolver.shared_addon_prefix}\w+/
      working_display 'Resetting' do
        return unless confirm_command
        output_with_arrow("Resetting password for #{db[:pretty_name]}")
        response = heroku_shared_postgresql_client(db[:url]).reset_password
        heroku.add_config_vars(app, {"DATABASE_URL" => response["url"]}) if db[:default]
      end
    else
      working_display 'Resetting' do
        return unless confirm_command
        output_with_arrow("Resetting password for #{db[:pretty_name]}")
        heroku_postgresql_client(db[:url]).rotate_credentials
      end
    end
  else
    uri = generate_ingress_uri
    display "Connection info string:"
    display "   \"dbname=#{uri.path[1..-1]} host=#{uri.host} port=#{uri.port || 5432} user=#{uri.user} password=#{uri.password} sslmode=require\""
  end
end
index() click to toggle source

pg

list databases for an app

# File lib/heroku/command/pg.rb, line 18
def index
  PGResolver::Resolver.all(config_vars).each {|db| display_db_info(db)}
end
info() click to toggle source

pg:info [DATABASE]

Display database information

defaults to all databases if no DATABASE is specified

# File lib/heroku/command/pg.rb, line 28
def info
  specified_db_or_all { |db| display_db_info db }
end
promote() click to toggle source

pg:promote [DATABASE]

Sets DATABASE as your DATABASE_URL

# File lib/heroku/command/pg.rb, line 36
def promote
  deprecate_dash_dash_db("pg:promote")
  follower_db = resolve_db(:required => 'pg:promote')
  error("DATABASE_URL is already set to #{follower_db[:name]}") if follower_db[:default]

  working_display "-----> Promoting #{follower_db[:name]} to DATABASE_URL" do
    heroku.add_config_vars(app, {"DATABASE_URL" => follower_db[:url]})
  end
end
psql() click to toggle source

pg:psql [DATABASE]

Open a psql shell to the database

(dedicated only) defaults to DATABASE_URL databases if no DATABASE is specified

# File lib/heroku/command/pg.rb, line 53
def psql
  deprecate_dash_dash_db("pg:psql")
  uri = generate_ingress_uri("-----> Connecting")
  ENV["PGPASSWORD"] = uri.password
  ENV["PGSSLMODE"]  = 'require'
  begin
    exec "psql -U #{uri.user} -h #{uri.host} -p #{uri.port || 5432} #{uri.path[1..-1]}"
  rescue Errno::ENOENT
    output_with_bang "The local psql command could not be located"
    output_with_bang "For help installing psql, see http://devcenter.heroku.com/articles/local-postgresql"
    abort
  end
end
reset() click to toggle source

pg:reset [DATABASE]

Delete all data in DATABASE

# File lib/heroku/command/pg.rb, line 71
def reset
  deprecate_dash_dash_db("pg:reset")
  db = resolve_db(:required => 'pg:reset')

  output_with_arrow("Resetting #{db[:pretty_name]}")
  return unless confirm_command

  working_display 'Resetting' do
    case db[:name]
    when %r\A#{Resolver.postgres_addon_prefix}\w+/
      display " database, related tables and functions...", false
      response = heroku_postgres_addon_client(db[:url]).reset_database
    when %r\A#{Resolver.shared_addon_prefix}\w+/
      display " database, related tables and functions...", false
      response = heroku_shared_postgresql_client(db[:url]).reset_database
    when "SHARED_DATABASE"
      heroku.database_reset(app)
    else
      heroku_postgresql_client(db[:url]).reset
    end
  end
end
unfollow() click to toggle source

pg:unfollow <REPLICA>

stop a replica from following and make it a read/write database

# File lib/heroku/command/pg.rb, line 98
def unfollow
  follower_db = resolve_db(:required => 'pg:unfollow')

  case follower_db[:name]
  when 'SHARED_DATABASE'
    output_with_bang "#{follower_db[:name]} does not support forking and following."
    return
  when %r\A#{Resolver.postgres_addon_prefix}\w+/
    output_with_bang "#{follower_db[:name]} does not support forking and following."
    return
  when %r\A#{Resolver.shared_addon_prefix}\w+/
    output_with_bang "#{follower_db[:name]} does not support forking and following."
    return
  else
    follower_name = follower_db[:pretty_name]
    follower_db_info = heroku_postgresql_client(follower_db[:url]).get_database
    origin_db_url = follower_db_info[:following]

    unless origin_db_url
      output_with_bang "#{follower_name} is not following another database"
      return
    end

    origin_name = name_from_url(origin_db_url)

    output_with_bang "#{follower_name} will become writable and no longer"
    output_with_bang "follow #{origin_name}. This cannot be undone."
    return unless confirm_command

    working_display "Unfollowing" do
      heroku_postgresql_client(follower_db[:url]).unfollow
    end
  end
end
wait() click to toggle source

pg:wait [DATABASE]

monitor database creation, exit when complete

defaults to all databases if no DATABASE is specified

# File lib/heroku/command/pg.rb, line 139
def wait
  specified_db_or_all do |db|
    case db[:name]
    when 'SHARED_DATABASE'
      next
    when %r\A#{Resolver.postgres_addon_prefix}\w+/
      next
    when %r\A#{Resolver.shared_addon_prefix}\w+/
      next
    else
      wait_for db
    end
  end
end