1165

Final update:

I had forgotten to run the initdb command.


By running this command

ps auxwww | grep postgres

I see that postgres is not running

> ps auxwww | grep postgres
remcat          1789   0.0  0.0  2434892    480 s000  R+   11:28PM   0:00.00 grep postgres

This raises the question:

How do I start the PostgreSQL server?

Update:

> pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting
sh: /usr/local/var/postgres/server.log: No such file or directory

Update 2:

The touch was not successful, so I did this instead:

> mkdir /usr/local/var/postgres
> vi /usr/local/var/postgres/server.log
> ls /usr/local/var/postgres/
server.log

But when I try to start the Ruby on Rails server, I still see this:

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

Update 3:

> pg_ctl -D /usr/local/var/postgres status
pg_ctl: no server running

Update 4:

I found that there wasn't any pg_hba.conf file (only file pg_hba.conf.sample), so I modified the sample and renamed it (to remover the .sample). Here are the contents:

 # IPv4 local connections:
 host    all             all             127.0.0.1/32           trust
 # IPv6 local connections:
 host    all             all             ::1/128                trust

But I don't understand this:

> pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting
> pg_ctl -D /usr/local/var/postgres status
pg_ctl: no server running

Also:

sudo find / -name postgresql.conf
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory

Update 5:

sudo pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Password:
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will own the server process.

Update 6:

This seems odd:

> egrep 'listen|port' /usr/local/var/postgres/postgresql.conf
egrep: /usr/local/var/postgres/postgresql.conf: No such file or directory

Though, I did do this:

>sudo find / -name "*postgresql.conf*"
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
/usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf.sample
/usr/share/postgresql/postgresql.conf.sample

So I did this:

egrep 'listen|port' /usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf.sample
#listen_addresses = 'localhost'        # what IP address(es) to listen on;
#port = 5432                # (change requires restart)
                # supported by the operating system:
                #   %r = remote host and port

So I tried this:

> cp /usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf.sample /usr/local/Cellar/postgresql/9.0.4/share/postgresql/postgresql.conf
> cp /usr/share/postgresql/postgresql.conf.sample /usr/share/postgresql/postgresql.conf

I am still getting the same "Is the server running?" message.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ramy
  • 20,541
  • 41
  • 103
  • 153
  • How did you install Postgres? Did you use a package manager or a manual installation? – James Allman Nov 02 '11 at 13:08
  • 1
    can't remember exactly but it was either installed already or i ran "brew install postgres". I'd lean towards the latter but again, i'm not 100% certain. – Ramy Nov 02 '11 at 13:19
  • Use `sudo`, ie `sudo pg_ctl...` – Bohemian Nov 04 '11 at 02:42
  • I have the same error, any updates on this? – codeAnand Nov 09 '11 at 05:47
  • @SAnandNarayan, in the end, i found that i needed to run the initdb command. How did you install postgres? – Ramy Nov 10 '11 at 16:32
  • @Ramy yes initdb solved the issue – codeAnand Nov 16 '11 at 08:11
  • I had this same issue. If your postgres server is running (check using ps aux | grep postgres) and psql still says server not running: creating a postgresql.conf file by copying the postgresql.conf.sample worked – viper May 02 '13 at 21:06
  • 63
    I up-voted this just because the FINAL UPDATE made me laugh very hard! :D – pkoch May 10 '13 at 00:53
  • 20
    Had to upvote, I come here at least 3 times a week to copy the very first pg_ctl command to restart psql after an unexpected termination.. heh I gotta learn it :D Thanks dude! – lucygenik Jun 17 '14 at 01:13
  • I simply followed [this](http://stackoverflow.com/questions/27700596/homebrew-postgres-broken) and it did the trick – James Franco May 03 '16 at 18:38
  • After stumbling on this issue every few months, I wrote a gist for myself in 2017 that I refer back to every time homebrew upgrades my psql version and leaves my postgres setup borked. Happy to share if it helps someone else! https://gist.github.com/hartleybrody/b0975ef3450995db58b247e1f5859a2d – Hartley Brody Sep 04 '20 at 17:33
  • 1
    If you are like me and think that you need to "start the database" when in fact all you want is to start working with the database then launch "pgAdmin". That's the equivalent of SQL Server Management studio for us Microsoft backgrounded folks. – P.Brian.Mackey Jul 13 '21 at 16:00

38 Answers38

2080

The Homebrew package manager includes launchctl plists to start automatically. For more information, run brew info postgres.

Start manually

pg_ctl -D /usr/local/var/postgres start

Stop manually

pg_ctl -D /usr/local/var/postgres stop

Start automatically

"To have launchd start postgresql now and restart at login:"

brew services start postgresql


What is the result of pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start?

What is the result of pg_ctl -D /usr/local/var/postgres status?

Are there any error messages in the server.log?

Make sure tcp localhost connections are enabled in pg_hba.conf:

# IPv4 local connections:
host    all    all    127.0.0.1/32    trust

Check the listen_addresses and port in postgresql.conf:

egrep 'listen|port' /usr/local/var/postgres/postgresql.conf

#listen_addresses = 'localhost'        # What IP address(es) to listen on;
#port = 5432                # (change requires restart)

Cleaning up

PostgreSQL was most likely installed via Homebrew, Fink, MacPorts or the EnterpriseDB installer.

Check the output of the following commands to determine which package manager it was installed with:

brew && brew list|grep postgres
fink && fink list|grep postgres
port && port installed|grep postgres
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
James Allman
  • 40,573
  • 11
  • 57
  • 70
  • i tried to 'touch /usr/local/var/postgres/server.log'. no luck though – Ramy Nov 02 '11 at 03:58
  • 1
    @Ramy Did the touch succeed? Check the existence and permissions of */usr/local/var/postgres* and *server.log*. It should be rw for your userid. – James Allman Nov 02 '11 at 04:04
  • 1
    @Ramy: Can you include any information in /usr/local/var/postgres/server.log? Your installation may be corrupt and it may be faster to clean up and re-install using a known source such as Homebrew. – James Allman Nov 04 '11 at 03:19
  • @Ramy: I updated my answer to include a method to determine how Postgres was installed. – James Allman Nov 04 '11 at 03:41
  • ok, this is getting ridiculous. i ran "brew list | grep postgres" and got a one line response: "postgres". So, i assumed this meant that I installed postgres with Homebrew. I then uninstalled postgres with "brew uninstall postgres" and then removed the postgres tarball from the cache in case it was corrupted during download ("rm /Users/remcat/Library/Caches/Homebrew/postgresql-9.0.4.tar.bz2" ). I then started the server with the the pg_ctl command you show in your answer. I made sure that the pg_hba.conf was updated properly. still getting the same "is the server running" message. – Ramy Nov 05 '11 at 22:20
  • I just had to `mkdir /usr/local/var/postgres` and it started right up. – Mark Richman Apr 29 '12 at 02:46
  • If this is your first install, create a database with: `initdb /usr/local/var/postgres -E utf8` ... Success. You can now start the database server using: `postgres -D /usr/local/var/postgres` – hagope Nov 30 '12 at 09:04
  • 12
    Why is all this necessary? Why can't you just start postgres the way you start node? – Kinnard Hockenhull Aug 05 '13 at 02:37
  • If you're trying to use an existing brew installed version after an upgarde you can switch to it as per http://stackoverflow.com/questions/3987683/homebrew-install-specific-version-of-formula I just needed `brew switch postgresql 9.2.1` as `which pg_ctl` was coming up blank. – toxaq Mar 03 '14 at 22:46
  • one nitpick `brew && brew list|grep postgres` doesn't actually display postgres from my brew list. However `brew list|grep postgres` does. – Harry Moreno Feb 13 '15 at 10:25
  • @JamesA Can you explain the syntax of the startup line `pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start` ? How exactly does this command work? – AdjunctProfessorFalcon Apr 19 '16 at 17:49
  • 65
    what a ridiculously complicated command just to start and stop the program – ahnbizcad Aug 18 '16 at 08:08
  • 1
    I literally have to look this up at least twice a week haha – luke Jun 14 '17 at 02:16
  • pg_ctl: server does not shut down – Evgeny Sep 05 '17 at 21:14
  • 8
    This still works but is a little out of date. The answer below `brew services start postgresql` is a more up to date, and straightforward answer. – mthorley Oct 05 '17 at 23:44
  • I discovered that using 'pg_ctl -D /usr/local/var/postgres start' threw permissions errors that enabled me to sort out why 'brew services start postgresql' stated the service was started even through postgresql service was not started. – Keith John Hutchison Dec 19 '17 at 08:06
  • Is it possible that they changed the folder name from `postgres` to `postgresql` recently? – chriszo111 Oct 09 '19 at 11:38
494

If you want to manually start and stop PostgreSQL (installed via Homebrew), the easiest way is:

brew services start postgresql

and

brew services stop postgresql

If you have a specific version, make sure to suffix the version. For example:

brew services start postgresql@10
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
malopezcruz
  • 601
  • 1
  • 5
  • 3
  • 8
    I wish I knew about `brew services` sooner... this is the only solution that's working for me. <3 thanks! – seanlinsley Jun 26 '14 at 14:15
  • 5
    `brew tap gapple/services` will make the brew services command work again – Fabian Jan 29 '15 at 19:17
  • 4
    Looks like `brew tap homebrew/services` still working https://github.com/Homebrew/homebrew-services – kangkyu Jul 16 '15 at 04:02
  • 4
    This works **after** `initdb` has been successfully run. Otherwise it silently fails (but appears to have worked, with the service showing as *started* in `brew services list`. – Dmitri Jun 24 '16 at 23:55
  • 2
    I needed to run `brew services restart postgresql`, since starting the db with pg_ctl or `brew services start` said "another server might be running" and and stoping the db with pg_ctl hung. – tomf Apr 08 '17 at 10:53
  • 2
    Important note - this will cause postgres to always start when your computer starts. If you want to start postgres as you please, use `pg_ctl` – s g Mar 14 '18 at 03:36
  • I also had to `rm /usr/local/var/postgres/postmaster.pid` per [this answer](https://stackoverflow.com/a/18832331/4754881). – Derek Soike May 16 '19 at 21:21
189

I had almost the exact same issue, and you cited the initdb command as being the fix. This was also the solution for me, but I didn't see that anyone posted it here, so for those who are looking for it:

initdb /usr/local/var/postgres -E utf8
Yan
  • 56
  • 1
  • 2
  • 5
  • 17
    this worked for me also, initdb told me that I had already the config done, so I deleted the entire directory: "rm -rf /usr/local/var/postgres" ran again your command and my server now gets up and running, many thanks sir! – Jorge Sampayo Jan 13 '13 at 17:57
  • 7
    yep I had to run `rm -rf /usr/local/var/postgres` and then this worked for me – Lee McAlilly Jan 14 '14 at 18:04
  • 2
    This is what worked for me as well. Before initdb, I had to also chown the postgres dir (`chown _your username on your comp_ usr/local/var/postgres`), then initdb. After that the server starts automatically when you restart (if you followed Homebrew's instructions for that) or manually `pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start`. – septerr Dec 11 '14 at 12:32
  • 2
    After `initdb` (including removing the old directory if necessary), `brew services start postgresql` works – Dmitri Jun 24 '16 at 23:58
  • If the idea of `rm -rf`ing everything that's currently in your database scares you a bit, instead of deleting it entirely, you could use something like `mv /usr/local/var/postgres /usr/local/var/postgres.backup` to move it to a new backup directory, then run the command to get the database started again. – Hartley Brody Feb 21 '17 at 05:23
126

If your computer was abruptly restarted


You may want to start PG server but it was not.

First, you have to delete the file /usr/local/var/postgres/postmaster.pid Then you can restart the service using one of the many other mentioned methods depending on your install.

You can verify this by looking at the logs of Postgres to see what might be going on: tail -f /usr/local/var/postgres/server.log

For specific version:-

 tail -f /usr/local/var/postgres@[VERSION_NUM]/server.log

Eg:

 tail -f /usr/local/var/postgres@11/server.log
rafaelportela
  • 306
  • 1
  • 5
  • 9
  • 2
    You can check `tail -f /usr/local/var/postgres/server.log` to see if you should delete that file. ref https://coderwall.com/p/zf-fww/postgres-on-osx-with-homebrew-not-running-after-osx-crash – user1448319 Nov 29 '16 at 01:02
  • Mine was under /Users//Library/Application\ Support/Postgres/var-9.6/postmaster.pid – BatteryAcid Jan 26 '17 at 22:11
  • for homebrew installs the path is `~/homebrew/var/postgres/postmaster.pid` – blnc Jun 10 '17 at 22:47
  • 2
    Wow... only this worked for me! Can you please give a more detailed explanation why we should delete `postmaster.pid` if the computer was abruptly restarted? – adkl Apr 01 '18 at 21:27
  • 1
    This was really useful – NinComPoop Oct 11 '18 at 06:50
  • 1
    my postmaster.pid was at "/usr/local/var/postgresql@9.6" as I have installed 9.6 version using brew. Removing postmaster.pid helped me as postgres did not start properly. then need to run "brew services restart postgresql@9.6" – Tharindu Jayasuriya Aug 15 '19 at 06:22
  • I cant start my pg using other answer, but after try to delete `/usr/local/var/postgres/postmaster.pid` like your answer, it's WORKED!!!... – Muhammad Dyas Yaskur Mar 04 '20 at 09:56
  • Removing `postmaster .pid` helps. Thank you! – Zernel Jun 04 '21 at 03:12
80

Another approach is using the lunchy gem (a wrapper for launchctl):

brew install postgresql
initdb /usr/local/var/postgres -E utf8
gem install lunchy

To start PostgreSQL:

lunchy start postgres

To stop PostgreSQL:

lunchy stop postgres

For further information, refer to: "How to Install PostgreSQL on a Mac With Homebrew and Lunchy"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pikachu
  • 774
  • 13
  • 15
78

Here my two cents: I made an alias for postgres pg_ctl and put it in file .bash_profile (my PostgreSQL version is 9.2.4, and the database path is /Library/PostgreSQL/9.2/data).

alias postgres.server="sudo -u postgres pg_ctl -D /Library/PostgreSQL/9.2/data"

Launch a new terminal.

And then? You can start/stop your PostgreSQL server with this:

postgres.server start
postgres.server stop
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kenial
  • 2,510
  • 23
  • 26
  • Specifying the data directory with -D is the key here – olore Feb 18 '14 at 05:02
  • 3
    I kept getting "Please log in (using, e.g., "su") as the (unprivileged) user that will own the server process." when running just sudo. +1 – pyb Feb 08 '15 at 18:38
  • 1
    Thanks. The postgres tools' suggestions to use su fail miserably. sudo -u works brilliantly. (on El Capitan) – uchuugaka Feb 08 '16 at 16:13
33

The cleanest way by far to start/stop/restart PostgreSQL if you have installed it through brew is to simply unload and/or load the launchd configuration file that comes with the installation:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

The first line will stop PostgreSQL and the second line will start it. There isn't any need to specify any data directories, etc. since everything is in that file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Markus Amalthea Magnuson
  • 8,415
  • 4
  • 41
  • 49
29

To start the PostgreSQL server:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

To end the PostgreSQL server:

pg_ctl -D /usr/local/var/postgres stop -s -m fast

You can also create an alias via CLI to make it easier:

alias pg-start='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start'
alias pg-stop='pg_ctl -D /usr/local/var/postgres stop -s -m fast'

With these you can just type "pg-start" to start PostgreSQL and "pg-stop" to shut it down.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Todd
  • 145
  • 3
  • 11
  • 6
    I got error `pg_ctl: directory "/usr/local/var/postgres" is not a database cluster directory` how to fix this? – Rohman HM May 05 '17 at 07:14
26

For test purposes, I think PostgreSQL App is the best option!

Run an app, and the server is up and running.

Close the app, and the server goes down.

http://postgresapp.com/

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Crystian Leão
  • 695
  • 1
  • 8
  • 18
14
# Remove old database files (if there was any)
$ rm -rf /usr/local/var/postgres

# Install the binary
$ brew install postgresql

# init it
$ initdb /usr/local/var/postgres

# Start the PostgreSQL server
$ postgres -D /usr/local/var/postgres

# Create your database
$ createdb mydb

# Access the database
$ psql mydb
psql (9.0.1)
Type "help" for help.
Goulven
  • 777
  • 9
  • 20
z atef
  • 7,138
  • 3
  • 55
  • 50
14

If you have installed using Homebrew, the below command should be enough.

brew services restart postgresql

This sometimes might not work. In that case, the below two commands should definitely work:

rm /usr/local/var/postgres/postmaster.pid

pg_ctl -D /usr/local/var/postgres start
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shanu khera
  • 170
  • 1
  • 12
13

Sometimes it's just the version which you are missing, and you are scratching your head unnecessarily.

If you are using a specific version of PostgreSQL, for example, PostgreSQL 10, then simply do

brew services start postgresql@10

brew services stop postgresql@10

The normal brew services start postgresql won't work without a version if you have installed it for a specific version from Homebrew.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1735921
  • 1,359
  • 1
  • 21
  • 46
10

When you install PostgreSQL using Homebrew,

brew install postgres

at the end of the output, you will see this methods to start the server:

To have launchd start postgresql at login:
    ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
    postgres -D /usr/local/var/postgres

I think this is the best way.

You can add an alias into your .profile file for convenience.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
WhiteDragon
  • 468
  • 6
  • 13
8

I had the same problem and performed all updates from the first post. But after checking the log file,

/usr/local/var/postgres/server.log

I see the true cause:

FATAL:  data directory "/usr/local/var/postgres" has group or world access
DETAIL:  Permissions should be u=rwx (0700).

After changing permissions on this directory,

chmod 0700 /usr/local/var/postgres

the PostgreSQL server started.

Check the log file every time.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Roosh
  • 964
  • 2
  • 9
  • 15
7

Variation on this answer: https://stackoverflow.com/a/13103603/2394728

initdb `brew --prefix`/var/postgres/data -E utf8`` &&  pg_ctl -D /usr/local/var/postgres/data -l logfile start
jackotonye
  • 3,537
  • 23
  • 31
substancejoy
  • 13
  • 1
  • 5
  • You should add a link to the answer you refer to. – Jeffrey Bosboom Feb 28 '15 at 01:10
  • This was helpful as my pg was working fine and all of sudden it stopped working and could not find /var/postgres folder. – shinesecret May 14 '15 at 08:38
  • Could someone please comment as to why this is superior to the linked answer? As I saw I already upvoted from the last time I had a similar problem I'm going to use it since there's no reason given here. – MCB Nov 19 '15 at 21:10
7

For a quick disposable test database, you can run the server in the foreground.

Initialize a new PostgreSQL database in a new directory:

mkdir db
initdb db -E utf8
createdb public

Start the server in the foreground (Ctrl + C to stop the server):

postgres -d db

In another shell session, connect to the server

psql -d public
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
k107
  • 15,882
  • 11
  • 61
  • 59
  • This seems great but doesn't work for me - it wasn't the server to already be running in order to do the createdb step. When I try to start the serve it says it needs the PGDATA or config-file, which don't exist yet. What am I missing? – szeitlin Sep 08 '16 at 20:54
6

If you didn't install it with Homebrew and directly from the Mac package, this worked for me for PostgreSQL 12 when using all the default locations, variables, etc.

$ sudo su postgres
bash-3.2$ /Library/PostgreSQL/12/bin/pg_ctl -D /Library/PostgreSQL/12/data/ stop
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
J_F4C
  • 1
  • 1
  • 4
5

PostgreSQL is integrated in Server.app available through the App Store in Mac OS X v10.8 (Mountain Lion). That means that it is already configured, and you only need to launch it, and then create users and databases.

Tip: Do not start with defining $PGDATA and so on. Take file locations as is.

You would have this file: /Library/Server/PostgreSQL/Config/org.postgresql.postgres.plist

To start:

sudo serveradmin start postgres

Process started with arguments:

/Applications/Server.app/Contents/ServerRoot/usr/bin/postgres_real -D /Library/Server/PostgreSQL/Data -c listen_addresses=127.0.0.1,::1 -c log_connections=on -c log_directory=/Library/Logs/PostgreSQL -c log_filename=PostgreSQL.log -c log_line_prefix=%t -c log_lock_waits=on -c log_statement=ddl -c logging_collector=on -c unix_socket_directory=/private/var/pgsql_socket -c unix_socket_group=_postgres -c unix_socket_permissions=0770

You can sudo:

sudo -u _postgres psql template1

Or connect:

psql -h localhost -U _postgres postgres

You can find the data directory, version, running status and so forth with

sudo serveradmin fullstatus postgres
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bbaassssiiee
  • 6,013
  • 2
  • 42
  • 55
5

For development purposes, one of the simplest ways is to install Postgres.app from the official site. It can be started/stopped from Applications folder or using the following commands in terminal:

# Start
open -a Postgres

# Stop
killall Postgres
killall postgres
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Melnosta
  • 11
  • 1
  • 1
5

This worked for me (macOS v10.13 (High Sierra)):

sudo -u postgres /Library/PostgreSQL/9.6/bin/pg_ctl start -D /Library/PostgreSQL/9.6/data

Or first

cd /Library/PostgreSQL/9.6/bin/
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4
  • install postgresql using brew: brew install postgresql, you can specify the version using "@" sign: brew install postgresql@14
  • start postgresql: brew services start postgresql or specific version brew services start postgresql@14
  • stop postgresql: brew services stop postgresql
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
3

If you installed PostgreSQL using the EnterpriseDB installer, then what Kenial suggested is the way to go:

sudo -u postgres pg_ctl -D /Library/PostgreSQL/{version}/data start
sudo -u postgres pg_ctl -D /Library/PostgreSQL/{version}/data stop
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hithacker
  • 151
  • 7
3

Homebrew is the way!!

To start the service:

brew services start postgresql

To list it:

brew services list | grep postgres

To stop the service:

brew services stop postgresql
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raj Verma
  • 1,050
  • 1
  • 7
  • 19
2

For MacPorts, just use the load/unload command and the port name of the running server:

sudo port load postgresql96-server
- or -
sudo port unload postgresql96-server

so you don't have to remember where the /Library/LaunchDaemons/org.macports.postgresql96.plist file is located.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steve
  • 836
  • 11
  • 14
2

If you didn't install the Postgres server with Homebrew or installed using .dmg file, try this:

$ sudo su postgres
bash-3.2$ /Library/PostgreSQL/13/bin/pg_ctl -D /Library/PostgreSQL/13/data/ stop
2

having installed Postgres with homebrew that is what I do to start postgres and keep it in foreground to see the logs:

/opt/homebrew/opt/postgresql/bin/postgres -D /opt/homebrew/var/postgres
Kevin Amiranoff
  • 13,440
  • 11
  • 59
  • 90
1

$ brew upgrade postgres

fixed it for me.

That, of course, will upgrade your PostgreSQL version and update/install any dependencies.

Warning: Do this knowing that your PostgreSQL version will likely change. For me, that wasn't a big deal.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
thedanotto
  • 6,895
  • 5
  • 45
  • 43
1

None of the previous answers fixed the issue for me, despite getting the same error messages.

I was able to get my instance back up and running by deleting the existing postmaster.pid file which was locked and was not allowing connections.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

This worked for me every time, inspired by Craig Ringer:

brew install proctools
sudo pkill -u postgres

proctools includes pkill. If you don't have Homebrew: https://brew.sh/

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Punnerud
  • 7,195
  • 2
  • 54
  • 44
1

After doing brew services restart postgresql.

It works best to: brew services stop postgresql brew postgresql-upgrade-database brew services start postgresql

Then type: psql

it now runs this was after the error: psql: error: 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"?

The upgrade may be optional depending on the other dependencies your running.

Which means that rather than Restart using brew for in on mac os, Stop completely postgres and then start postgres and connect to your psql databaseName.

Hope this was useful.

Paul S
  • 1
  • 1
  • 3
0

For Mac OS X, I really like LaunchRocket for this and other background services I used in development.

This site has nice instructions on installation.

This gives you a nice screen in your System Preferences that allows you to launch, reboot, root, and launch at login.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
theUtherSide
  • 3,338
  • 4
  • 36
  • 35
0
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

https://hub.docker.com/_/postgres

dewijones92
  • 1,319
  • 2
  • 24
  • 45
0

For completeness sake: Check whether you're inside a Tmux or Screen instance. Starting won't work from there.

From: Error while trying to start PostgreSQL installed via Homebrew: “Operation not permitted”

This solved it for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fr4nc3sc0NL
  • 555
  • 1
  • 6
  • 22
0

There is some edge case that maybe will be helpful for someone:

There is an option that you will have postgres.pid filled with some PID. If you restart your machine, and before PostgreSQL will be back again, some other process will take that PID.

If that will happen, both the pg_ctl status and brew service are asked about the PostgreSQL status, will tell you that it is up.

Just do ps aux | grep <yourPID> and check if it is really PostgreSQL.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Piotrowski
  • 674
  • 1
  • 7
  • 15
0

I was facing the same problem. I tried all of these solutions, but none worked.

I finally managed to get it working by changing the PostgreSQL HOST in Django settings from localhost to 127.0.0.1.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tazeem
  • 11
  • 1
0

There are two primary components to PostgreSQL: the database server and a client.

There is an included client via the CLI, or like me, you might be used to tools like phpMyAdmin, so it requires a separate GUI client.

For example, on macOS: install Postgres.app which is ~65 MB from: https://postgresapp.com/

Then follow these instructions:

  • install and initialise the server with the button on the right.
  • update your $PATH using terminal: sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp
  • and finally, install the pgAdmin GUI which is about ~100 MB from: https://www.pgadmin.org/download/pgadmin-4-macos/
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Scott Phillips
  • 177
  • 2
  • 10
0

Here the user that is executing the command is not a Postgres user. Because of that most people face Permission denied problem

> ./pg_ctl -D /Library/PostgreSQL/<version>/data start 
  pg_ctl: could not open PID file "/Library/PostgreSQL/<version>/data/postmaster.pid": Permission denied 

You have to switch to Postgres user and then need to execute the command

sudo -u postgres ./pg_ctl start -D /Library/PostgreSQL/<version>/data/ 
sudo -u postgres ./pg_ctl stop -D /Library/PostgreSQL/<version>/data/
sudo -u postgres ./pg_ctl reload -D /Library/PostgreSQL/<version>/data/ 
Harshad Panmand
  • 410
  • 5
  • 19
-1

Here is an easy and always forking solution (mac os):

(WARNING: IT WILL DELETE ALL YOUR DATABASES)

  1. Stop the postgres service:

    brew services stop postgres

  2. Delete all files in "postgres" directory:

    rm -rf /usr/local/var/postgres/*

  3. Initialize the new database system:

    initdb /usr/local/var/postgres -E utf8

  4. Start postgres:

    pg_ctl -D /usr/local/var/postgres -l server.log start

sn0rk
  • 83
  • 1
  • 5