I am trying to create postgres databases for development and tests. I'm using:
OSX Yosemite
Rails version: 4.2.0
git version: 2.2.2
psql version: 9.4.0
ruby version: 2.1.0p0
HomeBrew version: 0.9.5
Gemfile:
gem 'pg'
database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: myapp_development
username: username
password:
test:
<<: *default
database: myapp_test
rake db:create:all returns
PG::InsufficientPrivilege: ERROR: permission denied to create database
: CREATE DATABASE "myapp_development" ENCODING = 'unicode'
.... (lots of tracing)
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"myapp_development", "username"=>"username", "password"=>nil}
myapp_test already exists
What is wrong?
EDIT
I just tried changing the username in the database.yml to my username that I'm using on my Mac. It worked. It also told me that not only maybe_test already exists, but it also just told me that myapp_development already exists too.
Why wouldn't it be able to use the other username that I had created and assigned a role to CREATEDB?
Why did it say that the development couldn't be created then tell me that it already existed?
This all seems way too confusing and reminds me of PHP setup with Apache back in the very old days. I don't want to have to deal with problems every time I create a new app and try to follow the Heroku recommendations to use PostgreSQL during development too.
I have faced same issues when running rake db:test:prepare in postgresql on my Ruby on Rails project. This is pretty clear from the error message, that its a permission issue for the user. I added CREATEDB permission for new_user as following from the console.
To access postgres console:
$ sudo -u postgres -i
postgres#host:~$ psql
In there:
postgres=# ALTER USER new_user CREATEDB;
It's working perfect for now. You may have another issues with database ownership, for this you can change database privileges and owner as following command.
postgres=# GRANT ALL PRIVILEGES ON DATABASE database_name to new_user;
postgres=# ALTER DATABASE database_name owner to new_user;
Looking at your schema your credentials for development and test are different.
Perhaps remove username and password from the schema, seeing that your test database did get created.
create database demo;
create user demotest with password '123';
grant all privileges on database demo to demotest;
commit;
This is script for creation of database. But any existing database having password '123' then change your password for new database to password '1234'. This procedure working for me.
Related
As the title says, I clones a rails API. I tried to follow the steps in this article from point 2 onwards https://dev.to/w3ndo/a-checklist-for-setting-up-a-cloned-rails-application-locally-5468 but I keep getting the same error from db:setup onwards.
Please help!
I have tried googling the answer and phoning a friend.
I have tried rails db:setup, rails db:seed, rails db:create, rails db:migrate.
Update: So I found I was getting this error because the db owner was listed as the original owner in the repo but when I typed psql in terminal and located the db, the owner was listed as me.
I was able to change this using PGadmin 4 and type in the original owner as the db owner.
You want to initialize the postgres db, which doesn't quite come for free. I recommend using sqlite3 until you need a production db. If the clone calls for PG, then:
(user) $ sudo su - postgres
(postgres) $ createuser --interactive
(local) $ sudo systemctl restart postgresql
(local) $ bundle exec rails db:create:all
$ pg_isready tells you at a glance if posgtres server/cluster is online.
$ pg_isready
/tmp:5432 - accepting connections
If it gets frustrating, change the config/database.yml to the default version, remove pg gem if possible, add sqlite3. Then simple rake db:migrate after creating or adding an [environment].sqlite3 file to db/
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# 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: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
Update
Thank you for the help. I was able to find out as my partner had created the backend, the issue was arising because the owners of the database didn't match when I cloned his repo.
So I found I was getting this error because the db owner was listed as the original owner in the repo but when I typed psql in terminal and located the db, the owner was listed as me.
I was able to change this using PGadmin 4 and type in the original owner as the db owner.
I just created a new Ruby on rails project using this, in order to deploy it with Heroku
rails new -d postgresql LG_1
Then I used rails s to run my server and found this error :
role "esteban" does not exist.
I've been looking for solutions for an hour, but none of them worked. Using createuser just gives me the does-not-exist error, and I'm not very comfortable with Rails.
The versions I'm using :
Ruby 2.4.4
Rails 5.2.3
Pg (gem) 1.1.4
Here is my database.yml file (without the 75 lines of comments) :
default: &default
adapter: postgresql
encoding: unicode
host: /var/run/postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: LG_1_development
test:
<<: *default
database: LG_1_test
production:
<<: *default
database: LG_1_production
username: LG_1
password: <%= ENV['LG_1_DATABASE_PASSWORD'] %>
I did not edit this file.
EDIT : I found the solution. I had to open the pg_hba.conf file and manually add a new user called "esteban"
Did you install new Gem for project?
If you did I think what you didn't database migration.
Please run following commands.
$heroku run rake db:migrate
$heroku ps:scale web=1
$heroku ps
$heroku open
I guess esteban is the user name on your local machine and because you dont set specific username and password in database.yml for development mode postgres uses username of your user when trying to open connection to database.
Create role esteban in postgres:
# go to psql console under default postgres user
psql postgres
# create role
create role esteban SUPERUSER LOGIN;
# exit psql
\q
Or set correct one in database.yml if you already have it
I'm having problems assessing a postgres database from straight ruby.
I've created a Postgres database using Rails
>rails new www --database=postgresql
using Rails 4.2.5 and Postgres is 9.4
It produces the following config/database.yml file.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: www_development
test:
<<: *default
database: www_test
production:
<<: *default
database: www_production
username: www
password: <%= ENV['WWW_DATABASE_PASSWORD'] %>
I can run rails server, db:drop, db:create and db:migrate fine.
I can also access the database fine with psql
>psql www_development
But when I run the following app.rb from a non Rails project directory, I get a fe_sendauth: no password supplied (PG::ConnectionBad) error message.
It's clearly not a Rails issue. I've either missed something in my ruby or Postgres need a tweek to handle some difference between Rails and pure Ruby [that I'm not aware off]. I've also included Postgres' pg_hba.conf file.
At wits end trying to figure this one out. Any help would be much appreciated.
app.rb
require 'yaml'
require 'active_record'
require 'pg'
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'localhost',
database: 'www_development',
)
/etc/postgresql/9.4/main/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
You don't specify neither username, no password in your ActiveRecord::Base.establish_connection(. I assume you want to use some SUPERUSER without password for www_development database - right?
as per documentation peer auth does
Obtain the client's operating system user name from the operating
system and check if it matches the requested database user name.
That is why if you can psql without password, you should be able run app.rb with same OS user and environment without password. If you can't, then app.rb tries to connect with different username or so...
Options:
put username: postgres to ActiveRecord::Base.establish_connection(
change local all all peer to local all all trust
With ENV variable as password it's very likely that the variable itself is not present. Confirm the presence with printenv. You need to relogin/reboot for the variable to be accessible after you've included it in /etc/environment file for example. If this works, it's probably better than changing pg_hba.conf.
development:
<<: *default
database: postgres
username: postgres
password: postgres
# must specify the right DB, superuser and superuser Password as per postgreSQL setup
This worked for me, the only changes I needed to make. (ok fine i re-installed pgsql with 12.1)
I had this same issue when setting up a Rails 6 application.
The issue was that when start the rails server using rails server, and try to view the application from a browser, I get the error below:
ActiveRecord::ConnectionNotEstablished
fe_sendauth: no password supplied
Here's how I solved it:
The issue was caused by me not specifying/supplying the database password to be used by the application, so when the application tries to connect to the database specified in the config/database.yml it runs into that error.
I solved it by specifying the database password for the application in the config/database.yml:
# The password associated with the postgres role (username).
password: my-password
Also, ensure that you've created the database for the application if you've not created it already using:
rails db:create
Afterwhich I restarted the rails server:
rails server
This time when I tried viewing the application from the browser, it worked fine.
That's all.
I hope this helps
I was trying to use peer authentication which shouldn't ask for the password.
For me the issue was that I was specifying localhost in the database.yml so that was forcing the database driver to try to make a different type of connection (e.g., tcp, named pipe, etc)
Removing the "host: 'localhost'" line from my config solved the problem for me per this other answer: ActiveRecord connection to a localhost postgresql database in trust mode
Note that in my case I'm using the 'peer' method and not the 'trust' method but the solution is the same
On my OS X machine, I have a working Rails app (4.2) with Postgres (9.3).
Now, to create a second Rails app, would I need to create a new pg user?
In my first app, no username is provided in database.yml
development:
adapter: postgresql
database: my_first_app_development
pool: 5
timeout: 5000
Short answer no. You can use one user for many databases if that user be owner of this database or has superuser role.
I prefer to use one user in local machine that have superuser privileges.
$> sudo -u postgres psql
postgres# CREATE ROLE my_user CREATEDB SUPERUSER;
postgres# ALTER ROLE my_user WITH LOGIN;
Now you can create database with my_user owner:
postgres# CREATE DATABASE my_database WITH OWNER my_user;
Or just create database with a rake task but before add username to database.yml:
database: my_first_app_development
username: my_user
from shell:
$> bundle exec rake db:create
This good only for local development environment for production create separate user with right privileges.
OK, I'm building my first rails 3.0 app and want to test the postgreSQL server as production on my development machine (which is running 10.6). When you create a new app and rake db:migrate it creates sqlite db's for all three environments. Cool. Now I want to learn how to move to production and use postgres. I've used homebrew to install postgres, installed the pg (env ARCHFLAGS="-arch x86_64" gem install pg) and postgres-pr gems.
I've run rake db:migrate in hope that like with sqlite3 it will auto build my production server since I've updated my database.yml (see below).
OK, in my app's folder, I restart the server using 'rails s --environment=production' and it bails saying it cannot find my production database.
So all the google searches for 'rails 3 postgres install' got me this far, but I appear to be missing something because rails is failing to create the new pg database.
postgres is running as determined by ps.
createdb -Omysuperusername -Eutf8 vitae_production
createdb -Omysuperusername -Eutf8 /Users/sam/apps/vitae/db/vitae_production
But this directory does not have this database so I'm missing something. What am I overlooking?
this is my database.yml snippet:
production:
adapter: postgresql
host: localhost
database: db/vitae_production
pool: 5
timeout: 5000
username: mysuperusername
password:
There are a couple things going on here. First of all, you seem to be mixing the SQLite and PostgreSQL format for the database: setting in your database.yml. With SQLite, you specify the relative path to the SQLite database file with something like:
database: db/vitae_production.sqlite
but with PostgreSQL, you specify the database name with something like this:
development:
database: vitae_development
username: rails
...
Then, once database.yml is setup, you'd create the database user (if needed) from inside psql:
psql> create role rails login;
and then let Rails create the database:
$ rake db:create
Then you should be able to run your migrations to create your initial tables and away you go.
You probably want to read the create role documentation to make sure you get the right options. You probably want to be working in the development environment rather than production as well so I changed the names and YAML to reflect that, production is for deployment and I don't think you're deploying anything just yet.
Im'not familiar with rails but you could create your database as a postgres super user and then grant privileges, assuming that in your linux distribution the postgres super user is postgres:
su root
su postgres
createdb vitae_production
then in postgres grant privileges to another user
psql
CREATE USER rails WITH PASSWORD 'myPassword';
GRANT ALL PRIVILEGES ON DATABASE vitae_production TO rails;
ALTER DATABASE vitae_production OWNER TO rails;
then your config file should look like this:
production:
adapter: postgresql
host: localhost
database: vitae_production
pool: 5
timeout: 5000
username: rails
password: myPassword