Postgres drop database in rails app with rake command - ruby-on-rails

I am having trouble dropping my postgres database for a rails app - switched recently from sqlite after deploying to heroku. When running my rake task to aggregate data and populate the database, rake db:create db:migrate all works, but db:drop is not working. I am getting the following error in my terminal after running rake db:drop db:migrate db:create all. 'all' is a rake task for an aggregator.
Couldn't drop something_development : #<PG::Error: FATAL: database "postgres" does not exist
>
Couldn't drop something_test : #<PG::Error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
>
something_development already exists
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
'This is a look at my database.yml file...`
development:
adapter: postgresql
host: localhost
database: something_development
username: username
test:
adapter: postgresql
database: something_test
pool: 5
timeout: 5000
production:
adapter: postgresql
host: localhost
username: username
database: something_production
Also here is a list of databases from psql command line:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------------+-----------------+----------+---------+-------+-------------------------------------
username | username | UTF8 | en_US | en_US |
something_development | username | UTF8 | en_US | en_US |
template0 | username | UTF8 | en_US | en_US | =c/username+ | | | | | username=CTc/username
template1 | username | UTF8 | en_US | en_US | =c/username + | | | | | username=CTc/username
(4 rows)
EDIT - I am trying to do this in development before pushing to heroku. Do i need to add access privileges to my username? I tried GRANT UPDATE ON something_development TO username but received ERROR: relation "something_development" does not exist. Will remember that for Heroku - thanks.
Does anyone have any ideas on what might be happening or some resources that might point me in the right direction? Appreciate any help - thanks.

At first, you should make sure that which kind of postgres DB using on Heroku: Shared DB or Heroku Postgres.
If you are using Shared DB (it's recommended that shouldn't use any more), unfortunately you have to remove your Heroku app and re-create new one with the same app name.
If you are using Heroku PG, you could find your DB here: https://postgres.heroku.com/databases and just destroy and add another DB, it's like drop and re-create your DB, after that you can run migration as well. With Heroku PG, I'm not sure whether we can run db:drop now (It's been a while since I was fail to use this command on Heroku)
About configuration you should follow few steps below:
install gem pg
using pgAdmin III or something like that to try to connect to your PG server. If you're successful, use this information for your database.yml file.
Notice: When you push your code to Heroku, Heroku will automatically create new database.yml basing on their PG server information, so you don't need to care about configuration database on Heroku. Remember removing sqlite gem before pushing your app to Heroku

On heroku you don't have privileges for drop database, you have to do
heroku pg:reset
If your database is not on heroku, check the user (of postrgesql) has privileges.

Figured out the problem - turns out you cannot rename the database "postgres" because it needs that to manage the database. I had tried renaming postgres to something_development. To fix this, I created a postgres database.

Related

heroku run rails db:seed failed - Rails 7 + fixtures

I have successfully deployed my Rails app with Heroku, heroku run rails db:migrate, although heroku run rails db:seed failed.
Here is the error message
➜ quote-editor git:(master) heroku run rails db:seed
Running rails db:seed on ⬢ awesome-quote-editor... up, run.6832 (Free)
== Seeding the database with fixtures ==
WARNING: Rails was not able to disable referential integrity.
This is most likely caused due to missing permissions.
Rails needs superuser privileges to disable referential integrity.
cause: PG::InsufficientPrivilege: ERROR: permission denied: "RI_ConstraintTrigger_c_783704777" is a system trigger
rails aborted!
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "line_item_dates" violates foreign key constraint "fk_rails_048689f618"
DETAIL: Key (quote_id)=(309456473) is not present in table "quotes".
My setup
Ubuntu 20.04.2
Ruby 3.1.2
Rails 7.0.4
pg 1.4.3 with native extensions
I have both tried to give superuser privileges
ALTER USER myuser WITH SUPERUSER;
and
GRANT ALL PRIVILEGES ON DATABASE quote_editor_production TO myuser;
List of databases
Name | Owner | Encoding | Collate | Ctype | Access
----------------------------- | -------- | -------- | ------- | ------- | ------------
quote_editor_development | paulinetw | UTF8 | C.UTF-8 | C.UTF-8 |
----------------------------- | -------- | -------- | ------- | ------- | ------------
quote_editor_production | paulinetw | UTF8 | C.UTF-8 | C.UTF-8 | =Tc/paulinetw
quote_editor_test | paulinetw | UTF8 | C.UTF-8 | C.UTF-8 |
database.yml
default: &default
adapter: postgresql
encoding: unicode
development:
<<: *default
database: quote_editor_development
test:
<<: *default
database: quote_editor_test
production:
<<: *default
database: quote_editor_production
username: quote_editor
password: <%= ENV["QUOTE_EDITOR_DATABASE_PASSWORD"] %>
I have tried a lot of suggestions but still can't find a way out. I will really appreciate if somebody could help.
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "line_item_dates" violates foreign key constraint "fk_rails_048689f618" DETAIL: Key (quote_id)=(309456473) is not present in table "quotes".
Your seed file is trying to use a Quote with ID 309456473, but that quote doesn't exist.
UPDATE:
Here are my assumptions:
In producution, you run heroku run rails db:migrate, which works
In production, you run heroku run rails db:seed, which fails.
In your seed file you have some command that relies upon some code similar to Quote.find(309456473) and this fails in production
In your seed file, when you create Quotes, you are manually setting each quote's ID as you create it.
Setting the ID of your records on creation is not a good idea. Let the Db generate keys.
In your seed file, wherever you are looking up records by ID, find a different method to locate the record you want by some other attribute.
Don't assume that Heroku Postgres respects or preserves your record IDs from the seed file.

capistrano, rails, PG::ConnectionBad: fe_sendauth: no password supplied

I'm trying to run Capistrano for a rails app with Postgres on Ubuntu 14, I ran into a password error during rake db:migrate-
DEBUG [2823f146] Command: cd /home/ben/apps/mll/releases/20160414014303 && ( export RBENV_ROOT="$HOME/.rbenv" RBENV_VERSION="2.0.0-p645" RAILS_ENV="production" ; $HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate )
DEBUG [2823f146] rake aborted!
PG::ConnectionBad: fe_sendauth: no password supplied
I tried every solution to similar posts but no luck. For kicks I also tried running just that command in the remote app dir and got the following:
PG::ConnectionBad: FATAL: password authentication failed for user "mll"
FATAL: password authentication failed for user "mll"
This is interesting because it's using my database name as my username. See database.yml below, so for the hell of it I added a mll role, and lo and behold it worked when just running rake db:migrate. I tried running Capistrano again though with this new role, and still no luck.
Is it a reasonable guess that the username is not being accessed/stored properly? Any way for me to test that? I manually ALTER ROLE ben WITH PASSWORD 'mypw'; for both my ben and mll roles, and nothing.
My database.yml:
defaults: &default
adapter: sqlite3
encoding: utf8
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/development.sqlite3_test
production:
<<: *default
host: localhost
adapter: postgresql
encoding: utf8
database: mll
pool: 5
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
\du:
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
ben | Superuser, Create role, Create DB | {}
mll | Superuser, Create role, Create DB | {}
postgres | Superuser, Create role, Create DB, Replication | {}
I read changing md5 to trust helped some people, I tried that, but I'm unsure how to restart, all commands I've seen haven't worked for me.
pg_hba.conf:
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
You should use "trust" under method for localhost in pg_hba.conf. Note that this means all connections from localhost will be able to login as any user, which is probably fine as long as you're using this for development.
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
After changing your pg_hba.conf you can restart postgres with pg_ctl reload
The best practice to resolve the issue is -
Add the below gem in your Gemfile under development group and bundle install.
gem 'capistrano-postgresql'
Add the below line in Capfile
require 'capistrano/postgresql'
Add below lines in config/deploy.rb or in config/deploy/*.rb
set :pg_password, ENV['DATABASE_PASSWORD']
set :pg_ask_for_password, true

Where is my Rails database?

I paid someone to create a program with Ruby on Rails where it scrapes data and puts it in a Postgres database. In the program's directory are the standard Rails folders, application, "bin", "config" and other directories.
I'm trying to see a list of the columns in a table. I think the best, or only way, to do this is to log into the actual database, and print it out. I'm trying to use a "psql" command to log in but it is saying:
psql: FATAL: database "dan" does not exist
I'm not sure where the database is, or how I can find it.
This is what the config/database.yml contains:
development:
adapter: postgresql
database: danwork
pool: 5
timeout: 5000
encoding: unicode
username: dan
password: supersecretpassword
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
database: sports
pool: 5
timeout: 5000
encoding: unicode
username: namename
password: sports_db
Where is my database?
How could I find the database on my own using some linux commands, like find . -iname '...'?
How do I log in, and print out all the columns for the table named "games"?
You want rails dbconsole or rails db.
From the Rails Guide:
rails dbconsole figures out which database you’re using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.
You can also use the alias “db” to invoke the dbconsole: rails db.
To see the contents of your games table:
~/Documents/workspace/<project-dir>$ rails db
psql (9.6.3)
Type "help" for help.
development=# select * from games;
id | date | description | city | state
----+------+-------------+----------------------+-------
1 | | | Boston | MA
2 | | | Seattle | WA
(2 rows)
development=# \q
~/Documents/workspace/<project-dir>$
If you really want to use psql -U <username> <dbname>, you can find these parameters as described in this answer and paraphrased here:
~/Documents/workspace/<project-dir>$ rails c
Loading development environment (Rails 4.1.4)
2.3.1 :001 > Rails.configuration.database_configuration[Rails.env]
=> {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"development"}
2.3.1 :002 > exit
~/Documents/workspace/<project-dir>$ psql development
psql (9.6.3)
Type "help" for help.
development=# \q
~/Documents/workspace/<project-dir>$
As you can see, if no username is returned, you won't need the -U flag.
Your database is running on a server independent of Rails (it will be set up by your coder). It looks like your database is on your localhost.
It will be dependent on your server OS.
If you're looking to show column names in your database, you'll be able to do this in the Rails Console:
$ rails c
$ Game.column_names
To access your database using plain PostgreSQL
su username
psql
\db database_name;
To connect with psql, you must specify the database name.
If you're using password auth, simply psql -U username dbname.
If you're using peer auth, sudo -u username psql dbname.
In your case you seem to be using password auth and connecting to the local host, so I suggest:
psql -U dan danwork
and entering the password when prompted. (You can use a ~/.pgpass file to save the password if desired; see the pgpass documentation)

Why does Postgres authentication fail after deploying my Rails app to production?

It's taken me two days to get this far. But I'm almost there. When I run cap deploy:cold the deployment fails with this message:
servers: ["xxx"]
[xxx] executing command
** [out :: xxx] rake aborted!
** [out :: xxx] FATAL: password authentication failed for user "my_app"
My database.yaml has this:
production:
adapter: postgresql
encoding: utf8
database: my_app_production
pool: 5
host: localhost
username: my_app
password: secret
I'm running cap deploy under a user I created called deployer. I installed Postgres under deployer. I also created the user my_app in psql:
create user my_app with password 'secret';
create database my_app_production owner my_app;
I verified that the user my_app exists by running \du. When I ssh through deployer#xxx and I run the command psql I get psql: FATAL: role "deployer" does not exist.
What am I doing wrong?
For the psql invocation to be similar as what rails does, it should be:
psql -h localhost -U my_app -d my_app_production
If you omit these options, psql will take by default:
the OS username as the database username
the OS username as the database name
a Unix socket directory as the host (which differs from localhost and generally has different authentication rules in pg_hba.conf)
I've gone through the very same Railscasts you are going through I think. I just got my site up with it, though I am still having a couple glitches with cap deploy. In any case, for clarity.
You created a deployer user on the remote host obviously. But, that doesn't mean "deployer" is a user in Postgres. Once you ssh as deployer#xxx.xxx.xxx.xxx you have to run sudo -u postgres psql to enter psql as the default postgres admin. Presuming you're using Ubuntu, there is no default password.
Where I'm confused is how, if you can't get in on the remote system with deployer, did you create the my_app user in psql there? It almost sounds like you did this locally (Vagrant maybe?) and didn't do it remotely. As I said, I think I just finished the same stuff you're looking at and it works if the my_app user is created on the remote box with the correct password.

PostgreSQL rake db:create Error in local environment

I continue to run into issues whenever I use postgreSQL for Rails app development in my local environment.
I'm running on Mac OS X 10.7. I'm aware this version comes with a pre-installed Postgres version.
I've initially installed Postgres using the brew method and have, at least a few times, found success. I'm aware of the user authentication issues.
When punching in psql --version or which psql to determine which installation is being used, I've had to adjust my .bash_profile before to reflect the appropriate path with export PATH=/usr/local/bin:$PATH.
The issue I'm running into now, and hours among hours of Google haven't solved this, is that my machine has restarted without properly closing PostgreSQL. In turn, I am getting the following error when I try to execute rake db:create on a new application, I am getting the following error:
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"app_development", "pool"=>5, "username"=>"tomgeoco", "password"=>nil}
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
My config/database.yml looks like:
development:
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
username: tomgeoco
password:
test:
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
username: tomgeoco
password:
production:
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
username: tomgeoco
password:
When I check the status of Postgres servers with ps auxwww | grep postgre I get the following:
tomgeoco 12596 0.0 0.0 2434892 408 s000 R+ 11:54AM 0:00.00 grep postgres
So what could the problem be in this scenario?
This issue is not about rails but postgres. I faced with exactly the same problem recently with django project.
According to your ps output postgres isn't working at the moment.
First you have to check logs, my one is /usr/local/var/postgres/server.log. It'll give you an idea about what's wrong on postgre starting up.
My problem was solved by fixing PATH variable in .bash_profile.
Try to start your postgre server manually checking all its output. If everything goes fine but broken on system reboot, check out your launchctl daemons to find out which config file is used. This might be a problem as well.
Here you could find good instructions of clear installing of brew version of postgres. And here is modifying of postgres default settings which might be usefull as well.

Resources