I\'m using rails 4.1.5 with postgresql 9.1 under Debian 7, and I\'m not able to create a database in my development environment. When I run
bin/rake db:create
I see two issues, as is04 pointed out you probably need at least a username
value in your database.yml.
You also need to create the postgres role:
su - postgres
create role direct-dev with createdb login password 'password1'
Here is my Dockerfile for the rails project. Take a look at this part:
CMD mkdir -p tmp/pids && \
bundle exec rake db:create && \
bundle exec rails db:migrate && \
bundle exec rake db:seed && \
bundle exec puma -C config/puma.rb
Bundler 2.1.4, ruby 2.7.2, rails 6.0.3 compatible. Docker compose part:
server:
container_name: server
build:
context: Server
dockerfile: Dockerfile
args:
ENV: 'development'
restart: unless-stopped
depends_on:
- db
env_file:
- ./Server/.env
ports:
- '9090:3000'
volumes:
- ./Server:/app
- ./Server/Docker/usr/.gemrc:/root/.gemrc
networks:
- app-network
FROM ruby:2.7.2-alpine3.12
# env and arg variables setup
ARG APP_HOME='/app'
ARG ENV
ENV RAILS_ENV=$ENV \
RACK_ENV=$ENV \
RAILS_ROOT=$APP_HOME
# timezone setup
RUN apk add --update tzdata && \
cp /usr/share/zoneinfo/Europe/London /etc/localtime && \
echo "Europe/London" > /etc/timezone
# building in tmp dir
WORKDIR /tmp
ADD Gemfile ./
#install dependencies (bundler 2.1.4, ruby 2.7.2, rails 6.0.3 compatible)
RUN apk add --update --virtual runtime-deps postgresql-client nodejs libffi-dev readline sqlite xz && \
apk add --virtual build-deps build-base openssl postgresql-dev libc-dev linux-headers libxml2-dev libxslt-dev readline-dev && \
gem install bundler -v 2.1.4 && \
bundle install --jobs=4 && \
apk del build-deps
# /app
WORKDIR $APP_HOME
# create dbs if no such, migrate, seed, start
CMD mkdir -p tmp/pids && \
bundle exec rake db:create && \
bundle exec rails db:migrate && \
bundle exec rake db:seed && \
bundle exec puma -C config/puma.rb
I had the same problem and in my case, I had used Answer.column_name
in a validation in the Answer model itself. Because test database was already created on my local so RAILS_ENV=test rails db:create
was working fine on local, but giving the same error in CI/CD pipeline
It was due to the reason that Rails load all the files inside app/ directory before running db:create
command, and at that time as no database is there Answer.column_names
failed.
I used something like this:
validates_uniqueness_of :content, scope: (Answer.column_names - %w[id created_at updated_at]).map(&:to_sym), message: 'Duplicate answer'
which is wrong. Then I changed to:
DUPLICATE_ANSWER_SCOPE = %i[content question_id session_id]
validates_uniqueness_of :content, scope: DUPLICATE_ANSWER_SCOPE, message: 'Duplicate answer'
Rails 4.1 ships with spring preloader, and
New Rails 4.1 applications will ship with "springified" binstubs. This means that bin/rails and bin/rake will automatically take advantage of preloaded spring environments.
which means that the "springified" bin/rake
will attempt to preload the app, which in turn will attempt to run the initilizers resulting in the problem you're seeing.
To fix / work around this you want to run the initial setup rake tasks without spring. One way to achieve that is to run it with bundler instead:
bundle exec rake db:create