When trying to access my app locally, I am given: "could not translate host name ... to address" and "Temporary failure in name resolution" - ruby-on-rails

I am going through Heroku's getting started tutorial. As far as I know, Postgres is fully updated and everything is installed appropriately. Interestingly, the app works online when I deploy it to heroku, but when I try to run the app locally, not so much! I am getting a:
PG::ConnectionBad
could not translate host name "myname" to address: Temporary failure in name resolution
### Convenience alias for PG::Connection.new.
def self::connect( *args )
return PG::Connection.new( *args )
end
I have not touched any information in the files created by Heroku for this tutorial - so what could be wrong?
The following commands were run:
heroku login
git clone https://github.com/heroku/ruby-getting-started.git
cd ruby-getting-started
heroku create
git push heroku main
heroku open (this gets to the app okay) heroku logs --tail
which psql
bundle install
export DATABASE_URL=postgres://$(whoami) bundle exec rake db:create db:migrate (Here is where the error is)
heroku local web
(Error here) http://localhost:5000/

Debug Postgres connection
Confirm that Postgres exists in yout local machine, is running and it have a user that maches with you machine username.
Try to run this command in your local machine:
psql -U $(whoami) -d postgres
The output should be something like this:
psql (9.2.24, server 10.14)
WARNING: psql version 9.2, server version 10.0.
Some psql features might not work.
Type "help" for help.
postgres=#
Then, confirm that a database matching with your username exists with "\l".
postgres=#\l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
----------------+----------+----------+-------------+-------------+-----------------------
myuser | odoo | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
tip: you can close with "\q"
postgres=#\q
if no error, try replacing the value of DATABASE_URL with one of the following...
postgresql://
postgresql://localhost
postgresql://localhost:5433
postgresql://localhost/mydb
postgresql://user#localhost
If this is not successful, then you must review the way that postgres is installed.
...Or manually create a superadmin User with a password inside Postgres.
The conection string pattern is
postgres://user:password#host:port
example...
export DATABASE_URL=postgres://db_user:123456#localhost:5432

DATABASE_URL=postgres://$(whoami)
This is trying to connect to a computer which happens to have the same name as your local user account. Unsurprisingly, it doesn't exist, hence the error.

Here's the command to use if you are trying to make a backup or create a dump file from your local Postgres DB.
Mainly you need to change --host to --port, and that's it:
pg_dump --format t --no-owner --no-privileges --verbose --username postgres --password --dbname <DATABASE_NAME> --port <PORT, usually 5432> --file <THE FILE NAME YOU WANT>.dump
*if you are on Windows, use CMD instead of git bash. idk why.

Related

Trying to hunt down location of a string, associated with a postgres installation (connection option: "port")

I'm on a new-ish macbook: MacOs Big Sur.
I installed a local development environment via https://rails.new/.
For my day-job, we run everything in Docker, but I'm also doing ruby/rails develpment within that docker context.
I've had a beast of a time trying to connect to a local working postgres service.
Any time I try to run migrations, or connect via the terminal with psql, I get a similar error message, that makes me wonder if I fat-fingered a value into a text file somewhere, and don't know where:
rails db:migrate
rails aborted!
ActiveRecord::ConnectionNotEstablished: invalid integer value "5432export" for connection option "port"
Caused by:
PG::ConnectionBad: invalid integer value "5432export" for connection option "port"
Or when I run psql:
$ psql
psql: error: invalid integer value "5432export" for connection option "port
This happens even when I do rails new, so I think I messed up with something in a plist file or postgres config somewhere. It's not happening on my other laptop. This string doesn't exist in the results of rails new, so I assume it's inside of a postgres config file somewhere.
$ which psql
/usr/local/bin/psql
$ brew list postgres
/usr/local/Cellar/postgresql/14.3/bin/clusterdb
/usr/local/Cellar/postgresql/14.3/bin/createdb
/usr/local/Cellar/postgresql/14.3/bin/createuser
/usr/local/Cellar/postgresql/14.3/bin/dropdb
/usr/local/Cellar/postgresql/14.3/bin/dropuser
/usr/local/Cellar/postgresql/14.3/bin/ecpg
/usr/local/Cellar/postgresql/14.3/bin/initdb
/usr/local/Cellar/postgresql/14.3/bin/oid2name
/usr/local/Cellar/postgresql/14.3/bin/pg_amcheck
/usr/local/Cellar/postgresql/14.3/bin/pg_archivecleanup
/usr/local/Cellar/postgresql/14.3/bin/pg_basebackup
/usr/local/Cellar/postgresql/14.3/bin/pg_checksums
/usr/local/Cellar/postgresql/14.3/bin/pg_config
/usr/local/Cellar/postgresql/14.3/bin/pg_controldata
/usr/local/Cellar/postgresql/14.3/bin/pg_ctl
/usr/local/Cellar/postgresql/14.3/bin/pg_dump
/usr/local/Cellar/postgresql/14.3/bin/pg_dumpall
/usr/local/Cellar/postgresql/14.3/bin/pg_isready
/usr/local/Cellar/postgresql/14.3/bin/pg_receivewal
/usr/local/Cellar/postgresql/14.3/bin/pg_recvlogical
/usr/local/Cellar/postgresql/14.3/bin/pg_resetwal
/usr/local/Cellar/postgresql/14.3/bin/pg_restore
/usr/local/Cellar/postgresql/14.3/bin/pg_rewind
/usr/local/Cellar/postgresql/14.3/bin/pg_test_fsync
/usr/local/Cellar/postgresql/14.3/bin/pg_test_timing
/usr/local/Cellar/postgresql/14.3/bin/pg_upgrade
/usr/local/Cellar/postgresql/14.3/bin/pg_verifybackup
/usr/local/Cellar/postgresql/14.3/bin/pg_waldump
/usr/local/Cellar/postgresql/14.3/bin/pgbench
/usr/local/Cellar/postgresql/14.3/bin/postgres
/usr/local/Cellar/postgresql/14.3/bin/postmaster
/usr/local/Cellar/postgresql/14.3/bin/psql
/usr/local/Cellar/postgresql/14.3/bin/reindexdb
/usr/local/Cellar/postgresql/14.3/bin/vacuumdb
/usr/local/Cellar/postgresql/14.3/bin/vacuumlo
/usr/local/Cellar/postgresql/14.3/homebrew.mxcl.postgresql.plist
/usr/local/Cellar/postgresql/14.3/homebrew.postgresql.service
/usr/local/Cellar/postgresql/14.3/include/libpq/libpq-fs.h
/usr/local/Cellar/postgresql/14.3/include/postgresql/ (827 files)
/usr/local/Cellar/postgresql/14.3/include/ (23 files)
/usr/local/Cellar/postgresql/14.3/lib/libecpg.6.14.dylib
/usr/local/Cellar/postgresql/14.3/lib/libecpg_compat.3.14.dylib
/usr/local/Cellar/postgresql/14.3/lib/libpgtypes.3.14.dylib
/usr/local/Cellar/postgresql/14.3/lib/libpq.5.14.dylib
/usr/local/Cellar/postgresql/14.3/lib/pkgconfig/ (4 files)
/usr/local/Cellar/postgresql/14.3/lib/postgresql/ (91 files)
/usr/local/Cellar/postgresql/14.3/lib/ (17 other files)
/usr/local/Cellar/postgresql/14.3/share/doc/ (1143 files)
/usr/local/Cellar/postgresql/14.3/share/man/ (294 files)
/usr/local/Cellar/postgresql/14.3/share/postgresql/ (860 files)
I've reinstalled postgres via brew
and postgres seems to be running:
$ brew services
Name Status User File
postgresql none
postgresql#13 started username ~/Library/LaunchAgents/homebrew.mxcl.postgresql#13.plist
When I switch the running service, I get the same problem:
$ brew services stop postgresql#13
Stopping `postgresql#13`... (might take a while)
==> Successfully stopped `postgresql#13` (label: homebrew.mxcl.postgresql#13)
$ brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
$ psql
psql: error: invalid integer value "5432export" for connection option "port"
I am starting to try to run find commands, in my terminal, looking through the bin directory for the string 5432export but have not found it, and really don't know much about what lives inside of bin/psql, or even even if I'm looking in the correct general location.
I've tried different versions of ruby and rails, as well. Just can't get away from this error on this laptop!
Update:
When I run psql -p 5432, I get:
$ psql -p 5432
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: role "postgres" does not exist
And:
$ psql -p 5432 -U postgres
psql: error: connection to server at "127.0.0.1", port 5432 failed: FATAL: role "postgres" does not exist
Update 2:
Solved below! It was an environment variable issue, phew, thank you so much, this was driving me bonkers:
$ env | grep PG
PGHOST=127.0.0.1
PGPORT=5432export
PGUSER=postgres

Postgres server error on mac ["root" execution of the PostgreSQL server is not permitted. The server must be started under an unprivileged user ID]

Anyone to help me solve this issue that has been on my case for the last couple of hours? I have so far tried all suggested solutions but can't seem to find anything working.
Error:
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise. See the documentation for
more information on how to properly start the server.
I will forever be grateful to anyone who helps me solve this.
Try, change you user in postgres
Switching Over to the postgres Account
Switch over to the postgres account on your server by typing:
sudo -i -u postgres
You can now access a Postgres prompt immediately by typing:
psql
This will log you into the PostgreSQL prompt, and from here you are free to interact with the database management system right away.
Exit out of the PostgreSQL prompt by typing:
\q
This will bring you back to the postgres Linux command prompt.
Accessing a Postgres Prompt Without Switching Accounts
You can also run the command you’d like with the postgres account directly with sudo.
For instance, in the last example, you were instructed to get to the Postgres prompt by first switching to the postgres user and then running psql to open the Postgres prompt. You could do this in one step by running the single command psql as the postgres user with sudo, like this:
sudo -u postgres psql
This will log you directly into Postgres without the intermediary bash shell in between.
Again, you can exit the interactive Postgres session by typing:
\q
Many use cases require more than one Postgres role. Read on to learn how to configure these.
Steps to properly install Postgres via Homebrew on Mac M1:
brew install postgres [Maybe you need to use the alternative install method, as described here: https://docs.brew.sh/Installation#alternative-installs]
initdb /Users/<username>/db -E utf8
[This initializes postgres to use the given directory as the database directory. Normally it is not adviced to use the user directory for database storage.]
pg_ctl -D /Users/<username>/db -l logfile start
[After getting success with step 2 it will prompt to run step 3. This command manually starts the server.]

heroku pgbackups:restore not working on rails app

For some reason heroku pgbackups:restore won't work on my rails app,
I'm creating my dumpfile using the command:
pg_dump -F c -v -U john -h localhost project_development > -f dumpfile.dump
I then upload it to dropbox, as the heroku documentation says it needs to be needs to uploaded somewhere with an HTTP-accessible URL. After I've uploaded the file to dropbox, I then run the command
heroku pgbackups:restore HEROKU_POSTGRESQL_RED_URL 'https://dl.dropboxusercontent.com/content_link/JgHkmg0iv8vIMKWqlN6medJqjClPeWYl2AQqJWV8IeEiujnfYvPdtvq7xh26dvCQ'
the output of which is
Retrieving... done
Restoring... done
HEROKU_POSTGRESQL_RED_URL is my database url and the line in the single quotes is a raw version of my dumpfile(the dropbox documentation showed how to turn the dl file into a raw file).
But whenever go to my apps homepage I get an error, I can see from running heroku logs that the error is a database error
PG::UndefinedTable: ERROR: relation "listings" does not exist
LINE 1: SELECT "listings".* FROM "listings" ORDER BY listings.pri..
Also, when I run heroku pg:info I can see that nothing has actually been inserted into the database
=== HEROKU_POSTGRESQL_RED_URL (DATABASE_URL)
Plan: Hobby-dev
Status: Available
Connections: 2/20
PG Version: 9.3.4
Created: 2014-10-21 15:24 UTC
Data Size: 7.0 MB
Tables: 0
Rows: 0/10000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
Can anyone tell me why my database isn't being updated? Thanks.
I tested this on a blank hobby dev database with heroku. You don't need to include the the no-owner and no-acl options (-x -O) but it's considered good practice since those alter statements will fail.
The problem with your dump command is the stray >
pg_dump -F c -v -O -x -U john -h localhost project_development -f dumpfile.dump
Upload the resulting file to dropbox or s3 and try the restore again. If there are errors you can run
heroku logs -n 100 --ps pgbackups
To see the log output from the pg_restore command.

Automatically set username in database.yml for Postgres

When you generate a new rails app with postgres as db it automatically set username to appname in database.yml.
You then have to manually change it to your username or the app will not run.
Is there any way to automatically set it to logged in user (at time of running rails new) or other preset username?
In development and test environments it will always be the same username and production does not matter as it always deployed on heroku.
I am on ubuntu, but would be great to hear solutions for mac users also.
Ok I ended up doing this:
In my template I added this line to replace the username:
gsub_file "config/database.yml", /username: .*/, "username: myusername"
I have a simple workflow of sole developer on dev machine so usernames for developement and test will always be the same and production does not matter as it deploys to heroku.
It is a good habit to make a new user for each application. to do this, first create adminpack extension as follows:
$ psql postgres -c 'CREATE EXTENSION "adminpack”;'
Open database prompt to create a new user. lets say your app called blog:
$ psql -d postgres
postgres=# create role blog login createdb ;
postgres=# \q
Now create your database and associate that to the user you just made:
$ createdb -E UTF8 -w -O blog blog_development
To check of its created:
$ psql -l

Grabbing mysql dump file from remote server

Im working as an intern and am new to rails and it's production evn. I was wondering how I could grab a database dump from a remote server and import into my local database so that my local env mirrors that of the live version of a site. I have access to the database, and I have the current version of the code in my environment. I am missing the pictures and files attached to the site, and need it to make changes locally.
In the production server execute the following command
mysqldump -u username -ppassword db_name > production_dump.sql
scp the production_dump.sql file to your local machine
In your local machine execute the following command.
mysql -u username -ppassword db_name < production_dump.sql

Resources