How to restart Postgresql - postgresql

I have PostgreSQL 9.3 and 9.4 installed on my Linux Mint machine.
How can I restart PostgreSQL 9.4?
A method to restart both versions together is also fine.

Try this as root (maybe you can use sudo or su):
/etc/init.d/postgresql restart
Without any argument the script also gives you a hint on how to restart a specific version
[Uqbar#Feynman ~] /etc/init.d/postgresql
Usage: /etc/init.d/postgresql {start|stop|restart|reload|force-reload|status} [version ...]
Similarly, in case you have it, you can also use the service tool:
[Uqbar#Feynman ~] service postgresql
Usage: /etc/init.d/postgresql {start|stop|restart|reload|force reload|status} [version ...]
Please, pay attention to the optional [version ...] trailing argument.
That's meant to allow you, the user, to act on a specific version, in case you were running multiple ones. So you can restart version X while keeping version Y and Z untouched and running.
Finally, in case you are running systemd, then you can use systemctl like this:
[Uqbar#Feynman ~] systemctl status postgresql
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2017-11-14 12:33:35 CET; 7min ago
...
You can replace status with stop, start or restart as well as other actions. Please refer to the documentation for full details.
In order to operate on multiple concurrent versions, the syntax is slightly different. For example to stop v12 and reload v13 you can run:
systemctl stop postgresql-12.service
systemctl reload postgresql-13.service
Thanks to #Jojo for pointing me to this very one.
Finally Keep in mind that root permissions may be needed for non-informative tasks as in the other cases seen earlier.

You can also restart postgresql by using this command, should work on both the versions:
sudo service postgresql restart

On Windows :
1-Open Run Window by Winkey + R
2-Type services.msc
3-Search Postgres service based on version installed.
4-Click stop, start or restart the service option.
On Linux :
sudo systemctl restart postgresql
also instead of "restart" you can replace : stop or status.

This should work:
sudo systemctl stop postgresql
sudo systemctl start postgresql

The right way to restart postgres server is to use
pg_ctl restart -D /home/postgres/postgresdbdata/
pg_ctl is usually found in location usr/local/pgsql/bin/pg_ctl if u dont have it added to your path.
Documentation can be found here
https://www.postgresql.org/docs/13/app-pg-ctl.html
This has more details on Start,Stop,Restart,etc of the postgres server.

You can firstly check for the postgress process running
ps -ef | grep post
You might need to use (post|pg) on the grep side, to discover the process for the Postgres service running on your machine, it might depend on your OS.
And you can just kill the child process since most of this process should restart automatically.
sudo kill $pid_of_the_child_process

macOS:
On the top left of the MacOS menu bar you have the Postgres Icon
Click on it this opens a drop down menu
Click on Stop -> than click on start

Related

Starting a postgres SQL 9.6 Server on Amazon Linux returns unrecognized service

I am attempting to start a Postgres SQL server on amazon Linux using the command
sudo service postgresql start
I installed the server using this method. I have added it here for simplicity
sudo rpm -i https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-ami201503-96-9.6-2.noarch.rpm
and then
sudo yum install postgresql96-server.x86_64
after which i did this to install the command line tools for postgres
sudo yum install postgresql96.x86_64 postgresql96-libs.x86_64
Any suggestions on how I can start the server ? I usually start the server using
the command
sudo service postgresql start
however its not working in this case as it says "Unrecognized service"
I then tried this
postgres -D /usr/local/pgsql/data
postgres: could not access directory "/usr/local/pgsql/data": No such file or directory. Run initdb or pg_basebackup to initialize a PostgreSQL data directory.
Having the same issue, or similar. May be I installed pgsql from source, don't remember. We could make our own service start files. How? Let's find out! >>RTFM<< starting with what we already know:
man service
which leads us to chkconfig(8), so
man chkconfig
and it gives us an option
chkconfig --add ${svcname}
to add a brand new service under a name we choose!
But before we do, we might actually want to check what's already there. With
service --status-all
we get a list of all known services and their run status. And I found "postmaster" in my list, and as you might know, the PostgreSQL master server to connect to used to be called "postmaster". Yet, when I try
service postmaster status
it also tells me it doesn't know such service. OK, forget it -- for now -- just let's move on with making our own! But I still want to peek what there is in run-level 3 (normal server run level). So I go
ls -1 /etc/rc.d/rc3.d |fgrep post
and there I find: "K36postgresql95"! So, accordingly our service name should be "postgresql95". Trying that:
service postgresql95 status
it says now "postmaster is stopped". Confusingly the name the service reports for itself both in service --status-all and when we individually inquire for it is different than the name used to actually address it in the service command. Good to know. Easy enough to search /etc/rc.d for the name of interest.
service postgresql95 start
now starts the service. And check with
psql -U ${pguser} ${pgdb}
and I find that working. So now all I need to do is enable that service at system boot to auto-start
chkconfig --levels 3 postgresql95 on
and that works, doesn't it?
PS: It doesn't matter that I happen to run version 9.5
I recently installed PostgreSQL 9.2.24 on Amazon Linux 2 and I had to initialize the database manually before being able to create ROLE and DATABASE as I normally would on Ubuntu.
// initialize database after installing with yum
$ sudo postgresql-setup initdb
// start
$ sudo systemctl start postgresql.service

Postgresql installed through Brew does not start automatically

Story:
I installed postgresql using brew a while ago, but I haven't found a solution that it would start automatically on the startup. Do you have any solution?
Issue:
this is what I see when I try to connect to local database.
What I've tried:
This is $ brew services list output (single line)
postgresql started root /Library/LaunchDaemons/homebrew.mxcl.postgresql.plist
When installed, I followed instructions to start it now and after restart at login
Workaround:
Only workaround is to run this line
pg_ctl -D /usr/local/var/postgres start which is quite inconvenient since I have to look it up in info page or assign to alias which I can't remember each time when I restart the computer.
I realized most of these issues when my mac shutdown. To solve this, you may try this:
On your terminal run:  postgres -D /usr/local/var/postgres
Copy the PID returned and then run; kill -9 <PID>
Finally, run brew services restart postgres. That should do it for most of the cases

PostgreSQL Services

Can someone give a detailed explanation of how the postgresql service(s) works on Ubuntu [16.04]? The behavior that leads me to ask is the following. I use the command sudo systemctl start postgresql to start my postgresql service. When I run systemctl list-units | grep post I see
UNIT LOAD ACTIVE SUB DESCRIPTION
postgresql.service loaded active exited PostgreSQL RDBMS
postgresql#9.6-main.service loaded active running PostgreSQL Cluster 9.6-main
system-postgresql.slice loaded active active system-postgresql.slice
and when I run sudo systemctl stop postgresql both postgresql.service and postgresql#9.6-main.service disappear. What is each of these three services doing?
I assume postgresql#9.6-main.service is registered service for PostgreSQL Cluster 9.6-main cluster. This is the one that actually controls postgres on your system.
I assume postgresql.service is the effect desribed here
I assume system-postgresql.slice is a postgres slice unit
none of above are critical for running postgres. You can start/stop the cluser with just pg_ctl, without registering it with your services. But if you wonder which service runs it in your case - then it is postgresql#9.6-main.service

Psql could not connect to server: No such file or directory, 5432 error?

I'm trying to run psql on my Vagrant machine, but I get this error:
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"?
Note: Vagrant 1.9.2
Box: ubuntu/trusty64, https://atlas.hashicorp.com/ubuntu/boxes/trusty64
EDIT
Commands I've used in order to install and run postgres:
sudo apt-get update
sudo apt-get install postgresql
sudo su postgres
psql -d postgres -U postgres
I've had this same issue, related to the configuration of my pg_hba.conf file (located in /etc/postgresql/9.6/main). Please note that 9.6 is the postgresql version I am using.
The error itself is related to a misconfiguration of postgresql, which causes the server to crash before it starts.
I would suggest following these instructions:
Certify that postgresql service is running, using sudo service postgresql start
Run pg_lsclusters from your terminal
Check what is the cluster you are running, the output should be something like:
Version - Cluster Port Status Owner Data directory
9.6 ------- main -- 5432 online postgres /var/lib/postgresql/9.6/main
Disregard the '---' signs, as they are being used there only for alignment.
The important information are the version and the cluster. You can also check whether the server is running or not in the status column.
Copy the info from the version and the cluster, and use like so:
pg_ctlcluster <version> <cluster> start, so in my case, using version 9.6 and cluster 'main', it would be pg_ctlcluster 9.6 main start
If something is wrong, then postgresql will generate a log, that can be accessed on /var/log/postgresql/postgresql-<version>-main.log, so in my case, the full command would be sudo nano /var/log/postgresql/postgresql-9.6-main.log.
The output should show what is the error.
2017-07-13 16:53:04 BRT [32176-1] LOG: invalid authentication method "all"
2017-07-13 16:53:04 BRT [32176-2] CONTEXT: line 90 of configuration file "/etc/postgresql/9.5/main/pg_hba.conf"
2017-07-13 16:53:04 BRT [32176-3] FATAL: could not load pg_hba.conf
Fix the errors and restart postgresql service through sudo service postgresql restart and it should be fine.
I have searched a lot to find this, credit goes to this post.
Best of luck!
I had the same issue but non of the answers here helped.
How I fixed it (mac)
Try to start postgresql with pg_ctl -D /usr/local/var/postgres start
Look for the Error Message that says something like FATAL: could not open directory "pg_tblspc": No such file or directory.
Create that missing directory mkdir /usr/local/var/postgres/pg_tblspc
Repeat from step one until you created all missing directories
When done and then trying to start postgresql again it might say FATAL: lock file "postmaster.pid" already exists
Delete postmaster.pid: rm /usr/local/var/postgres/postmaster.pid
Start postgres with: pg_ctl -D /usr/local/var/postgres start
Done ✨
These two steps solved it for me on Mac:
rm /usr/local/var/postgres/postmaster.pid
brew services restart postgresql
For M1 Macs:
rm /opt/homebrew/var/postgres/postmaster.pid
brew services restart postgresql
In case you face this issue (reported by #luckyguy73): psql: FATAL: database "postgresql" does not exist
You can run
brew postgresql-upgrade-database
to fix it.
I am just posting this for anyone who is feeling lost and hopeless as I did when I found this question. It seems that sometimes by editing some psotgresql-related config files, one can accidentally change the permissions of the file:
Note how pg_hba.conf belongs to root, and users cannot even read it. This causes postgres to not be able to open this file and therefore not be able to start the server, throwing the error seen in the original question.
By running
sudo chmod +r pg_hba.conf
I was able to make this file once again accessible to the postgres user and then after running
sudo service postgresql start
Was able to get the server running again.
WARNING: This will remove the database
Use command:
rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8
WARNING: This will remove the database
Within zsh:
rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8
This is the only thing that worked for me after countless hours trouble shooting.
Does the /etc/postgresql/9.6/main/postgresql.conf show that port being assigned? On my default Xubuntu Linux install, mine showed port = 5433 for some reason as best as I can remember, but I did comment out the line in that same file that said listen_addresses = 'localhost' and uncommented the line listen_addresses = '*'. So maybe start and check there. Hope that helps.
This works for me:
pg_ctl -D /usr/local/var/postgresql#9.6 stop;
brew services stop postgresql#9.6;
brew services start postgresql#9.6;
I was able to solve the issue by running:
sudo systemctl start postgresql#9.5-main
In my case Postgres was managed through Homebrew Services (i.e. started via brew services start postgresql#10 Terminal command for the Postgres 10 that I use), and for that setup I had to discover a couple of essential steps to do before I could apply any advice in this thread. So I want to share just that piece as it may help someone who has the same setup.
NOTE: all the commands below are to be run in Terminal.
To give a quick background: After upgrading to macOS Big Sur I discovered that Postgres wasn't working and running psql results in the error mentioned in the original question above. I tried to start Postgres (via the brew services start postgresql#10 command), this resulted in a message Service postgresql#10 already started. If I tried to restart it (via the brew services restart postgresql#10) I got a message that it was stopped and then started successfully. But! This was a misleading message, and I spent quite some time searching for config issues etc. before discovering that the service was not started successfully in reality.
So, the way to investigate this is:
Make sure the service is started by running the brew services start postgresql#10 (the latter argument may be different depending on what your Homebrew package name is e.g. postgresql#12 or plain postgresql).
Run brew services list. This is the command that gives you the true state of the service. In my case it said that Postgres' status is error:
Name Status User Plist
postgresql#10 error Denis /Users/Denis/Library/LaunchAgents/homebrew.mxcl.postgresql#10.plist
redis started Denis /Users/Denis/Library/LaunchAgents/homebrew.mxcl.redis.plist
To investigate further open the config shown in the same command output in Plist column (I used nano /Users/Denis/Library/LaunchAgents/homebrew.mxcl.postgresql#10.plist to check it).
In the config look for the StandardErrorPath key, and open the file located in the value of that key, i.e. in the <string> tag following the key. In my case it was /usr/local/var/log/postgresql#10.log.
Open that log and check the latest error (I used nano /usr/local/var/log/postgresql#10.log and then Alt+/ to go to the end of the file).
Voila. That is the real error to investigate, which you can then look for in the previous answers or google for. I'm not covering the rest here, as the goal of this answer is to show how to find the real error if you use Homebrew Services to run Postgres. (In my case it was the lock file "postmaster.pid" already exists already covered in the previous answers, plus the path to check right in the error message, in my case /usr/local/var/postgresql#10).
In my case it was the lockfile postmaster.id that was not deleted properly during the last system crash that caused the issue. Deleting it with sudo rm /usr/local/var/postgres/postmaster.pid and restarting Postgres solved the problem.
I recommend you should clarify port that postgres.
In my case I didn't know which port postgres was running on.
lsof -i | grep 'post'
then you can know which port is listening.
psql -U postgres -p "port_in_use"
with port option, might be answer. you can use psql.
If non of the above answers are not working for you, then please try this one,
Many people have mentioned many solutions to this problem! But all of them forgot that, the same problem will arise when your disk don't have enough space or the space you are assigned for postgres is full
Check your system storage, if its full free up some space! then restart your postgres by sudo service postgresql restart or do a stop and start sudo service posgresql stop then sudo service postgresql start
This will solve the issue, it solved for me
I occasionally have the same issue but mostly after macOS upgrades. Shutting down and migrating to the new version usually fixes it for me(make changes according to your version). So first upgrade your postgresql
brew services stop postgresql#12
brew services start postgresql#12
brew postgresql-upgrade-database
This is mostly a temporary fix but since I couldn't find a better solution this works for me.
Update: If the issue says that another postmaster is running then try removing it from that location(your postmaster.pid location will be displayed to you)
rm /usr/local/var/postgres/postmaster.pid
Open your database manager and execute this script
update pg_database set datallowconn = 'true' where datname = 'your_database_name';
I had the same error when I create the SQL db in a VM. I had changed the default value of /etc/postgresql/9.3/main/postgresql.conf shared_buffers = 200MB to 75% of my total RAM. Well, I forgot to actually allocate that RAM in the VM. When I gave the command to make a new database, I received the same error.
Powered off, gave the baby its bottle (RAM) and presto, it worked.
The same thing happened to me as I had changed something in the /etc/hosts file. After changing it back to 127.0.0.1 localhost it worked for me.
just reinstall your pgsql with direct version sudo apt-get install postgresql-9.5 (u must remove the package before install new one)
I had similar problems just a while ago. After trying more than 5 suggestions I decided to go back to the basics and start from the beginning. Which meant removing my postgresql installation and following this guide upon re-installing postgresql. https://help.ubuntu.com/lts/serverguide/postgresql.html
Ubuntu 20
This Problem happened to me, as ubuntu pre-installed version of Postgresql-9.6 server was always down and after trying all the above answers it didn't start.
Solution:
I installed another version of Postgresql which is postgresql-13, using this command: sudo apt install postgresql it will install the latest version of postgresql.
I see if the server is online or down using this command: pg_lsclusters if the new version of postgresql is online, we will proceed to remove the old version of postgresql.
we will see all packages that are installed related to postgresql, using this command: dpkg -l | grep postgresql
Remove the old version, which is here postgresql-9.6. Using this command:
sudo apt-get --purge remove postgresql-9.6 postgresql-client-9.6 replace 9.6 with your old version number. Final remaining packages related to the latest Version 13:
Restart your postgresql latest version server, which is here postgresql-13. Using this command: sudo systemctl restart postgresql#13-main replace 13 in the command with your latest version number.
Now, if you try psql command you will get an error related to your user, as in the image:
To Remove the above error, The installation procedure created a user account called postgres that is associated with the default Postgres role, to switch over to the postgres account use this command: sudo -u postgres psql this command will log you into the interactive Postgres session. You can also set your password for this user using this command \password postgres.
Then change the Port to the deafult port of postgresql, which is 5432 as all application will try to connect to postgresql using this port by default, using this command: sudo nano /etc/postgresql/13/main/postgresql.conf, it will open postgresql configuration file, then search for port and change it to 5432. After that you need to restart the server using this command sudo systemctl restart postgresql#13-main. Note, Replace 13 in the command with your latest version.
If you want to create your own User/Role, use this command: sudo -u postgres createuser --interactive. The script will prompt you with some choices, as in the image and based on your responses, it will execute the correct Postgres commands to create a user to your specifications.
Tutorial: For more information on postgresql related commands
I couldn't connect using the psql command and kept getting the error Cannot connect to Server: No such file or directory.
Step 1: Check the status of the Postgres cluster
$ pg_lsclusters
Step 2: Restart the Postgres cluster
$ sudo pg_ctlcluster 12 main start
Make sure to replace 12 with your version of Postgres
Step 3: Check again and connect
$ pg_lsclusters
$ sudo -i -u postgres
$ psql
I got this error when I restored my database from last pg_basebackup backup file. After that when I tried to connect database(psql), I was getting the same error. The error was resolved, when I updated pg_hba.conf file and wherever "peer" authentication was there I replaced that with "md5" and then restarted postgres services. After that, the problem was resolved.
This error happened to me after my mac mini got un-plugged (so forced shutdown), and all I had to do to fix it was restart
I have the same issue with postgres 11 on my mac. I get this error every time after restart
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"?
As a temporary fix I do
brew services stop postgresql#11
brew services start postgresql#11
My problem happened after a brew update so I've ran
pg_ctl -D /usr/local/var/postgres start
and I've got this result:
FATAL: database files are incompatible with server 2021-07-07 13:27:21.692 CEST [70896] DETAIL: The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.2. stopped waiting
I've ran
brew postgresql-upgrade-database
FATAL: could not load server certificate file "/etc/ssl/certs/ssl-cert-snakeoil.pem": No such file or directory
LOG: database system is shut down
pg_ctl: could not start server
I have a missing ssl-cert-snakeoil.pem file so i created it using make-ssl-cert generate-default-snakeoil --force-overwrite And it worked fine.
In my case, I had to run journalctl -xe, and it showed that my disk was full. I then deleted some .gz items from /var/log and I could again restart the postgresql.
I'm on Kali Linux. I had to remove the brew version of postgresql with
brew uninstall postgresql
sudo -u postgres psql got me into root postgres
Simply running these commands from the installation steps in the official PostgreSQL docs worked for me (I'm on Fedora 33):
# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
sudo systemctl enable postgresql-13
sudo systemctl start postgresql-13
RHEL Installation link
kali users pls do this
sudo service postgresql restart

FATAL ERROR lock file "postmaster.pid" already exists

I have recently installed PostGIS on my Mac (El Capitan 10.11.4, Postgres is version 9.5.1) using Homebrew, and I am following these instructions - http://morphocode.com/how-to-install-postgis-on-mac-os-x/
When I try to start Postgres using
pg_ctl -D /usr/local/var/postgres start
I get the following error:
$ FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 280) running in data directory "/usr/local/var/postgres"?
So I spent a few hours researching how to address this, but to no avail.
Notably, I tried to kill the PID as recommended in an answer on Superuser - https://superuser.com/questions/553045/fatal-lock-file-postmaster-pid-already-exists- (in the case above, I ran kill 208), but as soon as I tried to start Postgres again, I got the same error, albeit with a different PID number. I saw a few people recommended deleting the postmaster.pid file, but I feel like maybe I should save that as a last resort...
Admittedly part of the reason I'm not sure how to fix this is that I'm not really clear on what the postmaster even is - I'm just starting to learn about all of this.
Hopping into a Postgres database via the psql db_name command works just fine, for what it's worth.
Posting this in case it helps someone else:
I was having this same problem as the OP after a hard reboot when my laptop crashed. What helped me was running the following command to see what PID was associated with postmaster.pid:
cat /usr/local/var/postgres/postmaster.pid
The first number that appears will be the PID. Looking in Activity Monitor, I was able to see that Postgres was running, but without a PID number that matched the one shown.
Instead of the steps outlined in the answer referenced on Superuser, I restarted my laptop properly and then opened up Terminal and ran
brew services restart postgresql
This worked without having to remove postmaster.pid, which I saw a few other posts recommend. Sometimes it's the simple solutions that work.
I add here what worked for me, after a long time of searching:
Delete the postmaster.pid file:
rm /usr/local/var/postgres/postmaster.pid
Restart your postgres:
brew services restart postgresql
Hope this helps someone ...
Update 8/2022:
As Mike commented, for M1 Mac you would replace stage 1 with:
rm /opt/homebrew/var/postgresql/postmaster.pid
With M1 and specify Postgres Version # 14
rm -rf /opt/homebrew/var/postgresql#14/postmaster.pid
It often happens to me in OSx, when my system shutdown unexpectedly.
You can just remove the file postmaster.pid.
cd Library/Application Support/Postgres/var-{postgres-version}
and remove the postmaster.pid file
in case you use brew then your path should be something like:
/usr/local/var/postgres/postmaster.pid
restart the Postgres by using this command
pg_ctl -D /usr/local/var/postgres restart
Since you can connect to the database, you don't need to start the server again - it's already running.
pg_ctl is used to control the PostgreSQL server. Since your server is already started, your command:
pg_ctl -D /usr/local/var/postgres start
Returns an error, saying that there is a lock on postmaster.pid - which is true since there is already a server running under that PID.
There are two ways:
The most basic way - skip that step, your server is already running!
Executing a needless operation - stopping the server, and then starting it again.
You could stop your server doing :
pg_ctl -D /usr/local/var/postgres stop
So that you won't have the lock on postmaster anymore and you could use your command to start it again.
Postmaster is the main PostgreSQL process. You're trying to start PostgreSQL that's already running (and you're saying yourself you can connect to it). Just skip that step of your process.
When the system shutdown unexpectedly, my postgres crashs and i'm unable to connect to it.
What worked for me was:
1˚ Check postgres log:
tail -n 10000 /usr/local/var/log/postgres.log
2˚ Find the PID of postgress, should look like this:
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 707) running in data directory "/usr/local/var/postgres"?
3˚ Kill that process:
kill 707
4˚ Restart your postgres
brew services restart postgresql
After those steps i was able to connect to the database within my rails application.
If you got no important data to lose :
sudo killAll postgres
brew services restart postgresql
AGAIN : You could get data corrupted by doing this !
do it at your own risk !
I am using mac and these step work for me:-
step1: cd Library/Application\ Support/Postgres
(most commonly your Postgres installation will be located here)
step2: cd var-13
(if you are using version 12 then use cd var-12. Hope got the point)
step3: ls
(As you can see among the files you find the postmaster.pid, perfect.)
step4: rm postmaster.pid
When you have removed the stale postmaster.pid file you can restart PostgreSQL and everything should work as normal.
My OSX laptop had shutdown unexpectedly, and I was getting a stale postmaster.pid error in the PostgresApp. Shutting down my laptop and turning it back on again solved the problem.
After running the following commands
rm /usr/local/var/postgres/postmaster.pid
brew services restart postgresql
The error lock file "postmaster.pid" already exists comes up again.
When we run launchctl list | grep postgres
28618 0 homebrew.mxcl.postgresql
The existing file "postmaster.pid" was created by this daemon process hosted by launchctl.
We try to stop the homebrew.mxcl.postgresql through
sudo launchctl stop homebrew.mxcl.postgresql
launchctl disable homebrew.mxcl.postgresql
Unfortunately, none of them could stop the homebrew.mxcl.postgresql.
The reason is Disable and enable an agent using (persists between boots)
https://apple.stackexchange.com/questions/105892/disable-services-in-osx-services-msc
launchctl enable <name> or launchctl disable <name>
Two ways to solve it when the error lock file "postmaster.pid" already exists comes up again
In order to stop an agent immediately through
launchctl kill homebrew.mxcl.postgresql
Restart your desktop and run brew services start postgresql#14. Now, PostgreSQL could start successfully.
Hope it could help someone who met the same issue again.
This worked for me. First locate postmaster.pid (for me it was in the var directory as seen below, although it will be different on depending on your operating system). Then get rid of postmaster.pid, then kill the postgres process, then start/restart postgres service.
cd /var/lib/pgsql/data/
rm postmaster.pid
sudo pkill -u postgres
sudo systemctl start postgresql.service
If you have installed postgres with brew then simply run the following command and it will manage everything
brew services restart postgresql