skip to Main Content

I’m a junior web developer trying to develop my first Ruby on Rails project. I’m following this guide to deploy the app on render.com.

However, after installing rails and following this command to create a database (I’m on Ubuntu):

rails db:create

I come across this problem in command window:


We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.

To resolve this issue:

- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.

To create your database, run:

        bin/rails db:create
Couldn't create 'mysite_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.

To resolve this issue:

- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.

To create your database, run:

        bin/rails db:create


Caused by:
PG::ConnectionBad: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
    Is the server running locally and accepting connections on that socket?

It says we couldnt find your database. But I have it in config/database.yml:


# PostgreSQL. Versions 9.3 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On macOS with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
#
default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: mysite_development

  # The specified database role being used to connect to postgres.
  # To create additional roles in postgres see `$ createuser --help`.
  # When left blank, postgres will use the default role. This is
  # the same name as the operating system user running Rails.
  #username: mysite

  # The password associated with the postgres role (username).
  #password:

  # Connect on a TCP socket. Omitted by default since the client uses a
  # domain socket that doesn't need configuration. Windows does not have
  # domain sockets, so uncomment these lines.
  #host: localhost

  # The TCP port the server listens on. Defaults to 5432.
  # If your server runs on a different port number, change accordingly.
  #port: 5432

  # Schema search path. The server defaults to $user,public
  #schema_search_path: myapp,sharedapp,public

  # Minimum log levels, in increasing order:
  #   debug5, debug4, debug3, debug2, debug1,
  #   log, notice, warning, error, fatal, and panic
  # Defaults to warning.
  #min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: mysite_test

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
#   production:
#     url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
  <<: *default
  database: mysite_production
  username: mysite
  password: <%= ENV["MYSITE_DATABASE_PASSWORD"] %>

What should I do? I’m going crazy over this.

I tried a different command:

        bin/rails db:create

also didn’t work.

3

Answers


  1. The error message indicates the server is not running.

    Did you:

    • Install the PostgreSQL packages?
    • Start the server? Try running:
      sudo systemctl start postgresql
    Login or Signup to reply.
  2. EDIT: Probably not the right answer for most people, since it seems to be a DB connection issue for OP. But this did catch me out. Hopefully someone else with the same circumstances as I can find this thread helpful in the future.

    First off, welcome to the industry & best of luck on your junior journey!

    I had this exact same problem just recently, and did a deep dive into the gems to debug the issue.

    Root cause

    I haven’t looked too closely after solving the problem, but to me it seems Rails 7 might be treating database.yml somewhat differently from previous versions of Rails (?). Or maybe the database.yml we are both using is messed up straight from the source. To be honest, not sure.

    Either way, the problem is this passage:

    default: &default
      adapter: postgresql
      encoding: unicode
      # For details on connection pooling, see Rails configuration guide
      # https://guides.rubyonrails.org/configuring.html#database-pooling
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
    

    ActiveRecord is treating default as an environment all on its own, at the same level as development, test and production

    Which means that, when you run rails db:create, there’s a connection going out for development, test, production…. and then one more time for default

    And since the database name is not set on default, ActiveRecord is intelligently using the default database for postgres… which happens to be called postgres

    Solution

    Simply remove the default config. I kind of like the &default / <<: *default pattern, so what I did was combine the default and development environments like so:

    development: &default
      <<: *default
      adapter: postgresql
      encoding: unicode
      # For details on connection pooling, see Rails configuration guide
      # https://guides.rubyonrails.org/configuring.html#database-pooling
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
      database: mysite_development
    

    That made everything work fine.

    Final file

    (Note: Untested. I am using my own config.)

    # PostgreSQL. Versions 9.3 and up are supported.
    #
    # Install the pg driver:
    #   gem install pg
    # On macOS with Homebrew:
    #   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
    # On macOS with MacPorts:
    #   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
    # On Windows:
    #   gem install pg
    #       Choose the win32 build.
    #       Install PostgreSQL and put its /bin directory on your path.
    #
    # Configure Using Gemfile
    # gem "pg"
    #
    
    development: &default
      <<: *default
      adapter: postgresql
      encoding: unicode
      # For details on connection pooling, see Rails configuration guide
      # https://guides.rubyonrails.org/configuring.html#database-pooling
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
      database: mysite_development
    
      # The specified database role being used to connect to postgres.
      # To create additional roles in postgres see `$ createuser --help`.
      # When left blank, postgres will use the default role. This is
      # the same name as the operating system user running Rails.
      #username: mysite
    
      # The password associated with the postgres role (username).
      #password:
    
      # Connect on a TCP socket. Omitted by default since the client uses a
      # domain socket that doesn't need configuration. Windows does not have
      # domain sockets, so uncomment these lines.
      #host: localhost
    
      # The TCP port the server listens on. Defaults to 5432.
      # If your server runs on a different port number, change accordingly.
      #port: 5432
    
      # Schema search path. The server defaults to $user,public
      #schema_search_path: myapp,sharedapp,public
    
      # Minimum log levels, in increasing order:
      #   debug5, debug4, debug3, debug2, debug1,
      #   log, notice, warning, error, fatal, and panic
      # Defaults to warning.
      #min_messages: notice
    
    # Warning: The database defined as "test" will be erased and
    # re-generated from your development database when you run "rake".
    # Do not set this db to the same as development or production.
    test:
      <<: *default
      database: mysite_test
    
    # As with config/credentials.yml, you never want to store sensitive information,
    # like your database password, in your source code. If your source code is
    # ever seen by anyone, they now have access to your database.
    #
    # Instead, provide the password or a full connection URL as an environment
    # variable when you boot the app. For example:
    #
    #   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
    #
    # If the connection URL is provided in the special DATABASE_URL environment
    # variable, Rails will automatically merge its configuration values on top of
    # the values provided in this file. Alternatively, you can specify a connection
    # URL environment variable explicitly:
    #
    #   production:
    #     url: <%= ENV["MY_APP_DATABASE_URL"] %>
    #
    # Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
    # for a full overview on how database connection configuration can be specified.
    #
    production:
      <<: *default
      database: mysite_production
      username: mysite
      password: <%= ENV["MYSITE_DATABASE_PASSWORD"] %>
    
    
    Login or Signup to reply.
  3. As default you are logged in as default user, That so this error happen. To resolve this error you have to logged in as a postgres user and create user with your username.

    sudo su -postgres
    

    then hit enter. then you will be logged in as a postgres user

    creatuser -s -r username
    

    then after you can switch back into default browser, to switch back hit ctrl+D
    then you can create database

    rails db:create
    

    vist this to get more know about this error https://www.udemy.com/course/professional-rails-5-development-course/learn/lecture/10575320#overview

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search