Postgresapp: Could not translate host name "localhost", service "5432" to address: nodename nor servname provided, or not known

Created on 7 Jul 2016  路  6Comments  路  Source: PostgresApp/PostgresApp

This is my first time installing Postgresql on this machine, I am using a MacBook Pro, OSX El Capitan v 10.11.5. I am trying to run a Rails application (https://github.com/ga-wdi-exercises/tunr_rails_json) but keep getting that log message:
Could not translate host name "localhost", service "5432" to address: nodename nor servname provided, or not known

...every time I try to run this command:
rake db:setup

I also get this error message on my terminal:

Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"tunr_test"} ** Invoke environment (first_time) ** Execute environment ** Execute db:schema:load_if_ruby ** Invoke db:schema:load (first_time) ** Invoke db:load_config ** Execute db:schema:load -- enable_extension("plpgsql") rake aborted! PG::ConnectionBad: 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"?

I have read the git issue #200 and #152 but was not able to get this up and running after trying to uninstall and install Postgresql several times...I am not wiping my whole system clean and re-installing my OSX like someone else there suggested, I am using a work laptop.

ping localhost

returns:
ping: cannot resolve localhost: Unknown host

dig localhost

returns:
; <<>> DiG 9.8.3-P1 <<>> localhost
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 59944
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 1

;; QUESTION SECTION:
;localhost. IN A

;; ANSWER SECTION:
localhost. 604800 IN A 127.0.0.1

;; AUTHORITY SECTION:
localhost. 604800 IN NS localhost.

;; ADDITIONAL SECTION:
localhost. 604800 IN AAAA ::1

;; Query time: 7 msec
;; SERVER: 192.168.1.1#53(192.168.1.1)
;; WHEN: Thu Jul 7 00:13:55 2016
;; MSG SIZE rcvd: 85

scutil -r localhost
Reachable

Most helpful comment

Looks like your computer can't resolve localhost. You probably fucked up your /etc/hosts file.

Let me quote from the Postico documentation:

This message is usually caused by a problem in your /etc/hosts file. The default contents of this file are (on OS X 10.11):

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost 

Reverting changes to /etc/hosts should fix this connection issue.

All 6 comments

Looks like your computer can't resolve localhost. You probably fucked up your /etc/hosts file.

Let me quote from the Postico documentation:

This message is usually caused by a problem in your /etc/hosts file. The default contents of this file are (on OS X 10.11):

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost 

Reverting changes to /etc/hosts should fix this connection issue.

I verified what my etc/hosts file said and it had most of the content deleted. You reminded me that in order to get an application version running locally for work, they had me delete the localhost and add our company address to it.

Ok, so after updating and restoring to my default /etc/hosts settings (http://www.imore.com/how-edit-your-macs-hosts-file-and-why-you-would-want) I tried running Postgres and it still gave me the same issue. I uninstalled Postgres using brew uninstall postgresql. I downloaded Postgres from their website directly http://postgresapp.com/ and I no longer received the logging errors from Postgres:
Could not translate host name "localhost", service "5432" to address: nodename nor servname provided, or not known

However, I am still not able to run the rake db:create command, I am getting a similar issue:
rake db:create 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"?

The Postgres Mac app is running fine on port 5432 and when I "open psql" I receive this error:
$ '/Applications/Postgres.app/Contents/Versions/9.5/bin'/psql -p5432 psql: FATAL: role "username" does not exist

I am looking into that rabbit hole now, but I think we're good to close this issue. Thanks @jakob for the quick response and help.

It sounds like the old server is still running. Quit Postgres.app, reboot your computer, then check in activity monitor if there are any processes named postgres. If there are, please uninstall them. Once you are sure that no PostgreSQL server is running on your Mac, start Postgres.app again.

Then make sure that the "Open psql" command works. If you keep getting the error that the db doesn't exist, quit Postgres.app, then delete the data directory (in ~/Library/Application Support/Postgres/), then restart Postgres.app.

Finally, make sure that you configured your path correctly (typing which psql should echo /Applications/Postgres.app/Contents/Versions/latest/bin/psql).

Awesome, so after that painstaking process I am able to get things up and running now!

I was able to get the "open psql" command to run properly, no more user errors being thrown my way. I was still receiving errors when running my "rake db:create" command, still with "rake db:create 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"?" but after adding this to my database.yml application file, I stopped receiving all errors!:
socket: /var/pgsql_socket/
host: localhost
Thank you for your patience and guidance @jakob, big props!

could be tell me where would i found database.yml to change host: postgres to host: localhost

Unable to connect to server:

could not translate host name "localhost " to address: Name or service not known

Was this page helpful?
0 / 5 - 0 ratings

Related issues

klouie93 picture klouie93  路  11Comments

markcerqueira picture markcerqueira  路  13Comments

chrisdel101 picture chrisdel101  路  4Comments

wellsoliver picture wellsoliver  路  4Comments

tarasinghmc picture tarasinghmc  路  7Comments