Unable to locate PostgreSQL folder - ruby-on-rails

I'm in the process of trying to find out or reset my PostgreSQL password, which is unknown to me, but which I need in order to migrate my database from SQLite3 to PostgreSQL.
I was trying to locate the hba_conf file so in the terminal I entered:
ps aux | grep postgres
and I found that the directory I needed to find was:
/Library/PostgreSQL/9.6/bin/postmaster -D/Library/PostgreSQL/9.6/data
My problem now is that it is not possible to locate this file because it apparently doesn't exist! When I cd to Library I'm unable to go any further because there is no PostgreSQL folder listed.
This is a bit of a dead end for me as I have no idea why PostgreSQL is not there. PSQL came with my version of Rails, and I updated it. When I type: 'psql -V' in the terminal, the answer is 'psql (PostgreSQL) 9.6.3'.
Help would be much appreciated, thanks :-)
From the library folder, if I run 'sudo su' then enter ls, I get the following:
.localized Calendars Dictionaries Internet Plug-Ins
Maps Saved Application State WebKit
Accounts CallServices Favorites Keyboard
Messages Screen Savers com.apple.nsurlsessiond
Address Book Plug-Ins ColorPickers FontCollections
Keyboard Layouts Metadata Services iMovie
Application Scripts Colors Fonts KeyboardServices
Passes Sharing
Application Support Compositions GameKit Keychains
PreferencePanes Sounds
Assistant Containers Google LanguageModeling
Preferences Spelling
Assistants Cookies Group Containers LaunchAgents
Printers Suggestions
Audio CoreData IdentityServices Logs
PubSub SyncedPreferences
Caches CoreFollowUp Input Methods Mail
Safari Voices
and if I enter ps I get this:
PID TTY TIME CMD
359 ttys000 0:00.02 login -pfl robertosullivan /bin/bash -c exec -
la bash /bin/bash
3267 ttys000 0:00.02 sudo su
3269 ttys000 0:00.01 su
3270 ttys000 0:00.00 sh
3271 ttys000 0:00.00 ps
If I try 'sudo find / -name psql' - I get:
find: /dev/fd/Library: No such file or directory
find: /dev/fd/Library: No such file or directory
/Library/PostgreSQL/9.6/bin/psql
/usr/local/bin/psql
/usr/local/Cellar/postgresql/9.6.3/bin/psql
When I try 'sudo find /Library/PostgreSQL/9.6/data -name *.conf' I get:
/Library/PostgreSQL/9.6/data/pg_hba.conf
/Library/PostgreSQL/9.6/data/pg_ident.conf
/Library/PostgreSQL/9.6/data/postgresql.auto.conf
/Library/PostgreSQL/9.6/data/postgresql.conf

The installation procedure creates a user account called postgres that is associated with the default Postgres role. In order to use Postgres, we can log into that account.
You can run the command you'd like with the postgres account directly with sudo
sudo -u postgres psql
This will prompt for the password for the postgres user.
If you don't have the password for this postgres user, follow the below steps:
sudo vim /etc/postgresql/9.3/main/pg_hba.conf
Around the line number 84,85 change that to
# Database administrative login by Unix domain socket
local all all trust
then Restart the PostgreSQL service via SUDO command
sudo /etc/init.d/postgresql restart
Now You can run the command to log in to the postgres account directly with sudo
sudo -u postgres psql
You will be now entered and will See the Postgresql terminal.Once you have successfully logged into postgres, you can change the password by the command
\password
and enter the NEW Password for Postgres default user, After Successfully changing the Password again go to the pg_hba.conf and revert the change to "md5"
Around the line number 84,85 change that now to
# Database administrative login by Unix domain socket
local all all md5
then Restart the PostgreSQL service via SUDO command
sudo /etc/init.d/postgresql restart
now you will be logged in as
psql -U postgres
with your new Password.
Please let me know if you have any Issues.

as we identified your data_dir as /Library/PostgreSQL/9.6/data/ and found configuration files in it, as we identified your postgres cluster is running with pg_ctl: server is running (PID: 88) /Library/PostgreSQL/9.6/bin/postgres "-D/Library/PostgreSQL/9.6/data, in order to reset your password do:
become a postgres user: sudo su - postgres
login locally with psql
reset the password with alter user USERNAME password 'NEW_PASSWORD'
after that you can connect as that user with psql -U USERNAME -h localhost using your new password

Related

403 forbidden nginx on docker/ddev website

I am attempting to deploy a site locally with Docker with DDev, using WSL2. I have gotten as far as getting phpMyAdmin working, and I have a ddev url, but when I try to use ddev launch and/or goto the http://sitename.ddev.site I get a 403 error.
So far I have tried to change docroot to public and that didn't work. Additionally I have SSH'd into the docker container, and all of the appropriate files are there. My directory hierarchy looks like (using ls in the powershell):
~/dev/project_name$ ls
composer.json composer.lock project_name public sites vendor
Additionally, and this may be a separate question and possibly unrelated - but in the event that it isn't - when I run the drupal installation command with:
ddev exec drush site:install --account-name=admin --account-pass=admin
I get asked for database name/driver/username/password/host/port - of which I'm not totally sure what information it expects me to supply. I managed to find the database information in Docker, but I haven't been able to correctly supply it the correct information, apparently. The input and output:
Database name [drupal]:
> db
Database driver [mysql]:
> mysql
Database username [drupal]:
> db
Database password [drupal]:
> root
Database host [127.0.0.1]:
> localhost
Database port [3306]:
> 3306
[warning] Program UNKNOWN not found. Proceed if you have already created or emptied the Drupal database.
Do you want to continue? (yes/no) [yes]:
> yes
[notice] Starting Drupal installation. This takes a while.
[notice] Performed install task: install_select_language
[notice] Performed install task: install_select_profile
[notice] Performed install task: install_load_profile
[notice] Performed install task: install_verify_requirements
In install.core.inc line 971:
Database name field is required.
Database username field is required.
Resolve all issues below to continue the installation. For help configuring your database server, see the <a href="https://www.dr
upal.org/docs/8/install">installation handbook</a>, or contact your hosting provider.<div class="item-list"><ul><li>Failed to con
nect to your database server. The server reports the following message: <em class="placeholder">SQLSTATE[HY000] [2002] No such fi
le or directory</em>.<ul><li>Is the database server running?</li><li>Does the database exist or does the database user have suffi
cient privileges to create the database?</li><li>Have you entered the correct database name?</li><li>Have you entered the correct
username and password?</li><li>Have you entered the correct database hostname and port number?</li></ul></li></ul></div>
Failed to execute command drush site:install --account-name=admin --account-pass=admin: exit status 1```
The next notice should be this:
[notice] Performed install task: install_verify_database_ready
Maybe the database is down, not connected, or the access data is wrong.
Check the drush status:
ddev exec drush st
Drupal version : 9.3.2
Site URI : http://my-drupal9-site.ddev.site
DB driver : mysql
DB hostname : ddev-my-drupal9-site-db
DB port : 3306
DB username : db
DB name : db
Database : Connected
Steps to reproduce, without the error
install brew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
install ddev
brew install drud/ddev/ddev
start container
cd ~/dockertests/
mkdir my-drupal9-site
cd my-drupal9-site
ddev config --project-type=drupal9 --docroot=web --create-docroot
ddev start
ddev composer create "drupal/recommended-project"
ddev composer require drush/drush
ddev drush site:install -y
ddev drush uli
ddev launch
# [success] Installation complete. User name: admin User password: XXXX
install drupal again
ddev exec drush site:install --account-name=admin --account-pass=admin

Configuring Username and Password in PostgreSQL

I have just reset my password in psql by editing my pg_hba.conf file to
local all all trust
before changing it back to
local all all md5
after. After doing this I tried to restart the psql server but I found that I have no access to any the server through the pg_ctl commands, for example if I try:
pg_ctl -D /Library/PostgreSQL/9.6/data restart
The reply is:
pg_ctl: could not open PID file
"/Library/PostgreSQL/9.6/data/postmaster.pid": Permission denied
The only way that I'm able to open psql is with this command:
psql -U postgres
Because if I try just 'psql', the response is the following:
psql: FATAL: role "robertosullivan" does not exist
I need to find some way of connecting to psql with my username and my new password so that I can use psql with my app in Rails.
Any advice would be much appreciated, thanks.
First of all, "robertosullivan" does not exist. Which means that you have configured your PG with a user account that simply isn't there. Seems like the easiest way around this would be to create the user like so:
sudo -u postgres createuser robertosullivan -s
sudo -u postgres psql
postgres=# \password your-password
Below query might help you
psql -h localhost -U postgres -d postgres

How to import PG dump from digitalocean to local machine?

I am new to rails.
I want to take backup of pg database from digitalocean to my local machine. How I take dump of that and migrate to my local machine
To use pg_dump,
First, for the target machine(remote machine with database you want to dump), two steps to make the machine receive pg_dump requests:
1.Add or edit the following line in your postgresql.conf :(in my experience, the location maybe /etc/postgresql/9.3/main/postgresql.conf, replace 9.3 with your psql version. If nobody change the file before, you add the line below to the end of the file)
listen_addresses = '*'
2.Add the following line as the first line of file 'pg_hba.conf'. (in my experience, the location like /etc/postgresql/9.3/main/pg_hba.conf) It allows access to all databases for all users with an encrypted password:
# TYPE DATABASEUSER CIDR-ADDRESS METHOD
host all all all md5
After those two steps, type in the terminal:
/etc/init.d/postgresql start
At last, in your local machine, you should figure out the target database's user(or owner) who can read it:
You can achieve this by ssh to connect that machine and step into psql console
sudo su - postgres && psql
and type
\l
to see the db owner.
Finally you can use pg_dump in your local machine to dump the database.Like :
pg_dump -f dump_name -h host_ip -d database_name -U database_user -p 5432 -W
then input the user's password, and wait for the long time for dumping the db.
Hope you make it~
First you need to create backup then download dump from digital ocean and the run these commands on console.
Download dump using SCP.
1-pg_dump dbname > outfile
2-pg_restore --verbose --clean --jobs=4 --disable-triggers --no-acl --no-owner -h localhost -U user_name -d database_name outfile.dump

Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

Before anything, please note that I have found several similar questions on Stack Overflow and articles all over the web, but none of those helped me fix my issue:
PG Error could not connect to server: Connection refused Is the server running on port 5432?
PG::ConnectionBad - could not connect to server: Connection refused
psql: could not connect to server: Connection refused
Now, here is the issue:
I have a Rails app that works like a charm.
With my collaborator, we use GitHub to work together.
We have a master and an mvp branches.
I recently updated my git version with Homebrew (Mac).
We use Foreman to start our app locally.
Now, when I try to launch the app locally, I get the following error:
PG::ConnectionBad at /
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
I tried to reboot my computers several times.
I also checked the content of /usr/local/var/postgres:
PG_VERSION pg_dynshmem pg_multixact pg_snapshots pg_tblspc postgresql.conf
base pg_hba.conf pg_notify pg_stat pg_twophase postmaster.opts
global pg_ident.conf pg_replslot pg_stat_tmp pg_xlog server.log
pg_clog pg_logical pg_serial pg_subtrans postgresql.auto.conf
As you can see, there is no postmaster.pid file in there.
Any idea how I could fix this?
run postgres -D /usr/local/var/postgres and you should see something like:
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 379) running in data directory "/usr/local/var/postgres"?
Then run kill -9 PID in HINT
And you should be good to go.
You most likely ran out of battery and your postgresql server didn't shutdown correctly.
The easiest workaround is to download the official postgresql app and launch it: it will force the server to start (http://postgresapp.com/)
Most likely it's because your system shutdown unexpectedly
Try
postgres -D /usr/local/var/postgres
You might see
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 449) running in data directory "/usr/local/var/postgres"?
Then try
kill -9 PID
example
kill -9 419
And it should start postgres normally
The postgresql server might be down and the solution might be as simple as running:
sudo service postgresql start
which fixed the issue for me.
This could be caused by the pid file created for postgres which has not been deleted due to unexpected shutdown. To fix this, remove this pid file.
Find the postgres data directory. On a MAC using homebrew it is /usr/local/var/postgres/, other systems it might be /usr/var/postgres/
Remove pid file by running:
rm postmaster.pid
Restart postgress. On Mac, run:
brew services restart postgresql
I had almost just as same error with my Ruby on Rails application running postgresql(mac). This worked for me:
brew services restart postgresql
This worked in my case:
brew uninstall postgresql
rm -fr /usr/local/var/postgres/
brew install postgresql
In my case PostgreSQL updates from version 13.4 to 14 in background, so it fixes by:
brew postgresql-upgrade-database
In other case the problem fixed by:
rm -rf /usr/local/var/postgres/postmaster.pid
or
rm -rf /opt/homebrew/var/postgres/postmaster.pid
Restart service postgresql:
brew services restart postgresql
PS:
How can you understand what is the problem?
For first see what service is not correct started:
brew services list
For second show file postgres.log, where will be the error:
tail -f /usr/local/var/log/postgres.log
or
tail -f /opt/homebrew/var/log/postgres*
And so find answer by this error's text
I resolved the issue via this command
pg_ctl -D /usr/local/var/postgres start
At times, you might get this error
pg_ctl: another server might be running; trying to start server anyway
So, try running the following command and then run the first command given above.
pg_ctl -D /usr/local/var/postgres stop
Step 1:
cd /etc/postgresql/12/main/
open file named postgresql.conf
sudo nano postgresql.conf
add this line to that file
listen_addresses = '*'
then open file named pg_hba.conf
sudo nano pg_hba.conf
and add this line to that file
host all all 0.0.0.0/0 md5
It allows access to all databases for all users with an encrypted password
restart your server
sudo /etc/init.d/postgresql restart
This is how I solved my problem:
see the status of services
brew services list
and the output was :
Name Status User Plist
postgresql error myuser /Users/myuser/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
unbound stopped
I change the file name in this path, you can also remove it
mv /Users/myuser/Library/LaunchAgents/homebrew.mxcl.postgresql.plist /Users/myuser/Library/LaunchAgents/homebrew.mxcl.postgresql.plist_temp
and then reboot the os
sudo reboot
after booting I started the postgresql and it worked.
brew services start postgresql
Find postgresql#10 service directory
$ ls /usr/local/var/postgresql#10
Find file postmaster.pid and delete that file
$ rm -f postmaster.pid
Restart postgres service using
$ brew services restart postgresql#10
This worked for me:
run
sudo lsof -i :<port_number>
after that it will display the PID which is currently attached to the process.
After that run sudo kill -9 <PID>
if that doesn't work,
try the solution offered by user8376606 it would definitely work!
If you want to restart Postgresql on Linux, then you have to use the following command.
/etc/init.d/postgresql restart
In my case
I have changed the port in postgresql.conf file
and restart postgresql services in
Run => service.msc => Restart
now retry
First I tried
lsof -wni tcp:5432
but it doesn't show any PID number.
Second I tried
Postgres -D /usr/local/var/postgres
and it showed that server is listening.
So I just restarted my mac to restore all ports back and it worked for me.
For Docker users: In my case it was caused by excessive docker image size. You can remove unused data using prune command:
docker system prune --all --force --volumes
Warning: as per manual (docker system prune --help):
This will remove:
all stopped containers
all networks not used by at least one container
all dangling images
all dangling build cache
I encountered a similar problem when I was trying to connect my Django application to PostgreSQL database.
I wrote my Dockerfile with instructions to setup the Django project followed by instructions to install PostgreSQL and run Django server in my docker-compose.yml.
I defined two services in my docker-compose-yml.
services:
postgres:
image: "postgres:latest"
environment:
- POSTGRES_DB=abc
- POSTGRES_USER=abc
- POSTGRES_PASSWORD=abc
volumes:
- pg_data:/var/lib/postgresql/data/
django:
build: .
command: python /code/manage.py runserver 0.0.0.0:8004
volumes:
- .:/app
ports:
- 8004:8004
depends_on:
- postgres
Unfortunately whenever I used to run docker-compose up then same err. used to pop up.
And this is how my database was defined in Django settings.py.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'abc',
'USER': 'abc',
'PASSWORD': 'abc',
'HOST': '127.0.0.1',
'PORT': '5432',
'OPTIONS': {
'client_encoding': 'UTF8',
},
}
}
So, In the end I made use of docker-compose networking which means if I change the host of my database to postgres which is defined as a service in docker-compose.yml will do the wonders.
So, Replacing 'HOST': '127.0.0.1' => 'HOST': 'postgres' did wonders for me.
After replacement this is how your Database config in settings.py will look like.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'abc',
'USER': 'abc',
'PASSWORD': 'abc',
'HOST': 'postgres',
'PORT': '5432',
'OPTIONS': {
'client_encoding': 'UTF8',
},
}
}
I often encounter this problem on windows,the way I solved the problem is Service - Click PostgreSQL Database Server 8.3 - Click the second tab "log in" - choose the first line "the local system account".
It also gives the same error if you just stop your PostgreSQL app. You just need to start it again. (PostgreSQL 11)
I have faced the same issue and I was unable to start the postgresql server and was unable to access my db even after giving password, and I have been doing all the possible ways.
This solution worked for me,
For the Ubuntu users:
Through command line, type the following commands:
1.service --status-all (which gives list of all services and their status. where "+" refers to running and "-" refers that the service is no longer running)
check for postgresql status, if its "-" then type the following command
2.systemctl start postgresql (starts the server again)
refresh the postgresql page in browser, and it works
For the Windows users:
Search for services, where we can see list of services and the right click on postgresql, click on start and server works perfectly fine.
In my case I forgot to change the database from postgres (on my production) back to sqlite3 which I was using for development.
It's working for me >>Node.Js App
user#MacBook-Pro % sudo lsof -i :5430
Output
COMMAND PID user FD TYPE DEVICE SIZE/OFF NODE NAME
node 7885 user 21u IPv6 0x2e7d89f6118f95b9 0t0 TCP *:radec-corp (LISTEN)
Kill the PID
user#MacBook-Pro % sudo kill -9 7885
One more test
user#MacBook-Pro % sudo lsof -i :5430
user#MacBook-Pro % "No more running PID for the port 5430"
In my case, on a Ruby on Rails project, I removed a .pid file from the folder tmp/pids and restart the system.
Had the same issue. I checked that my database.yml file, (dev mode) host was pointing to 5433. I updated it to 5432 and it worked.
Just in case someone needs this for windows, read on.
On windows hit the Windows button + R
then enter services.msc and look for postgresql-x64-14, Right click it and click start
Then go back to your PgAdmin4 for windows and then enter your master password if asked.
From here, you should be able to proceed as usual with viewing of the db schemas.
Also, for Django, restart your server with CTR+C then python manage.py runserver (assuming you're working inside a virtual env)
Good luck
ps -ef|grep postgres
Then kill the process with PID
sudo kill -9 PID
Then start the postgresql
sudo service postgresql start
In my case I when it happens to me I need to do the following steps:
1º Step
Log in postgres user:
#sudo su postgres
2º Step
Run the following steps: /opt/PostgreSQL/10/bin/postgres -D /opt/PostgreSQL/10/data -r /usr/local/var/postgres/server.log
Explanation:
We access the utility of postgres located at /opt/PostgreSQL/10/bin/ in your case could be another but identify where it's.
After this step we tell to the utility of postgres where the it's data folder is by using the option -D, this data folder contains all necessary configuration of postgres server.
The option -r we tell to postgres where to send stdout and stderr to given file, in my case the file that I used is /usr/local/var/postgres/server.log
Note:
I'm using Postgre 10
Linux Ubuntu

error with postgresql datababse : Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

When I run the rake db:migrate or run the rails s command, I get the same error:
Error : could not connect to server:
No such file or directory Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I get the error in the browser when I try rails s.
This is my database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: books_development
test:
<<: *default
database: books_test
production:
<<: *default
database: books_production
username: abd
password: <%= ENV['BOOKS_DATABASE_PASSWORD'] %>
Note : I have the databases books_development; books_test
; and the postresql are running without problems when I try sudo /etc/init.d/postgresql start
I did run:
create database books_development;
create database books_test;
in the psql console. And it said that it's done successfully
I tried a lot of solutions and I spent yesterday looking for a solution and no solution in the related questions solved my error.
I have postgresql-9.4 (the latest) and xubuntu 14.04
Any Ideas?
The convention for PostgreSQL packaged for Debian or Debian derivatives such as Ubuntu is to use /var/run/postgresql as the directory for Unix domain sockets. On the other hand the convention for self-compiled postgres client libs is to use /tmp, unless self-configured otherwise.
So the usual root cause of this mismatch between both is a mix of self-compiled client-side stuff with pre-compiled server-side packages (even if client and server are installed on the same machine, client-side and server-side are still distinct and can be out of sync).
Soft-linking from /tmp to this directory as suggested by the asker works except that the link will be lost at every reboot, because in general /tmp is emptied on reboot.
A better option would be to add as an entry in database.yml:
either host: /tmp if the real socket path is /tmp (self-compiled server, packaged client)
or host: /var/run/postgresql if the real socket path /var/run/postgresql/ (packaged server, self-compiled client).
When the value in the host field starts with a slash character, the postgres library knows that it's the location of a directory for local sockets rather than a hostname. The filename inside the directory .s.PGSQL.portnumber is generated and must not be specified, only the directory.
Another possibility is to configure the self-compiled software packages as closely as possible to Debian, overriding the defaults as they do.
I had the same Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”? error when typing psql into the postgres user in Ubuntu 14.04. I could not find an existing working solution.
The short answer for me was: my install made a var/pgsql_socket directory but no configuration files knew about it.
1) Find the postgres.conf file (it was in etc/postgresql/9.6/main for me)
2) change to listen_addresses = '*'
3) add another unix socket directory
unix_socket_directories = '/var/run/postgresql, /var/pgsql_socket' # comma-separated list of directories
4) at this point, sudo service postgresql start attempted to start but did not have authority to create the lock file.
* The PostgreSQL server failed to start. Please check the log output:
2016-10-05 17:14:55 CEST [28472-1] FATAL: could not create lock file "/var/pgsql_socket/.s.PGSQL.5432.lock": Permission denied
2016-10-05 17:14:55 CEST [28472-2] LOG: database system is shut down
5) Change permissions ( found from Mark Berry's comment here )
$ sudo chown root.postgres /var/pgsql_socket
$ sudo chmod g+wx /var/pgsql_socket
6) sudo service postgresql start sudo -i -u postgres psql
That finally worked for me
That means your Postgres server is not running.
Check Postgres Service status from Terminal
sudo service postgresql status
Enable Postgres Service, If not started
sudo service postgresql start
OR
sudo service postgresql restart
Now your command should work, If Postgres Service is successfully started.
I solved It . I Just created a softlink using :
sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
and then edited the
/etc/postgresql/9.4/main/pg_hba.conf
( If you have another version of postgresql you have to change 9.4 in the path)
From:
local all postgres peer
To:
local all postgres md5
Solution:
Try this
export LC_ALL="en_US.UTF-8"
and this. (9.3 is my current PostgreSQL version. Write your version!)
sudo pg_createcluster 9.3 main --start
The exact same symptom can be caused by a stale lock file /var/run/postgresql/.s.PGSQL.5432.lock. One of the symptoms of this is psql reporting
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
even though there is clearly a socket with this path available as reported by netstat -lp --protocol=unix | grep postgres
The problem can be solved by removing the lock file and restarting postgresql. This is definitely less invasive than a purge and re-install.
sudo rm /var/run/postgresql/.s.PGSQL.5432.lock
sudo service postgresql restart
On Mac OS X I usually get this error when my computer shuts down incorrectly, for example, due to power failure.
The solution I use is pretty simple and works 100% of the time:
# Find the postgres config folder
cd /usr/local/var/postgres
# remove file
rm postmaster.pid
# restart postgres
brew services restart postgres
Running pg_lsclusters will list all the postgres clusters running on your device
eg:
Ver Cluster Port Status Owner Data directory Log file
9.6 main 5432 online postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log
if the status is down run
#format is pg_ctlcluster <version> <cluster> <action>
sudo pg_ctlcluster 9.6 main start
If this process is not successfull it will throw the error.
My error was(You can see the error log on /var/log/postgresql/postgresql-9.6-main.log)
FATAL: could not access private key file "/etc/ssl/private/ssl-cert-snakeoil.key": Permission denied
Try adding `postgres` user to the group `ssl-cert`
make sure that postgres is the owner of /var/lib/postgresql/version_no/main
eg: sudo chown postgres -R /var/lib/postgresql/9.6/main/
It happened to me and it turned out that I removed erroneously the Postgres user from "ssl-cert" group. Run the below code to fix the user group issue and fixing the permissions
#set user to group back with
sudo gpasswd -a postgres ssl-cert
# Fixed ownership and mode
sudo chown root:ssl-cert /etc/ssl/private/ssl-cert-snakeoil.key
sudo chmod 740 /etc/ssl/private/ssl-cert-snakeoil.key
# now postgresql starts! (and install command doesn't fail anymore)
sudo service postgres restart
When I run into this error, my Postgres server was actually listening on a different port (5433) and not 5432.
To solve this, add
port: 5433
to your database.yml file to instruct rails to use the same

Resources