5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Rails & PostgreSQL]rails db:createをするとPG::InsufficientPrivilege: ERROR: permission denied to create databaseが発生

Posted at

Postgres初心者です。エラーを解決した備忘録として残しておきます。

環境

Amazon Linux2
Rails 6.0.3
Posgres 11.5

起きていること

本番環境でrails db:createを入力したところ以下のようなエラーが発生した

$ rails db:create RAILS_ENV=production
PG::InsufficientPrivilege: ERROR:  permission denied to create database
Couldn't create 'travelour_production' database. Please check your configuration.
rails aborted!
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR:  permission denied to create database
bin/rails:4:in `<main>'

Caused by:
PG::InsufficientPrivilege: ERROR:  permission denied to create database
bin/rails:4:in `<main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)

##解決方法
どうやらデータベースの作成権限がないらしい。そこでpostgresにログインして、権限を付与していく

#ログイン
$ psql -U postgres

ALTER ROLEコマンドでデータベース作成権限を与える

postgres=# ALTER ROLE {ユーザ名} WITH CREATEDB;
ALTER ROLE

その後serviseコマンドで再起動

 sudo service postgresql restart

これでrails db:createコマンドができるようになります。

rails db:create RAILS_ENV=production
Created database `travelour_production`
5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?