Reputation: 21261
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.
Upvotes: 1194
Views: 1300182
Reputation: 8388
brew install postgresql
, you can specify the version using "@" sign: brew install postgresql@14
brew services start postgresql
or specific version brew services start postgresql@14
brew services stop postgresql
Upvotes: 9
Reputation: 633
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/
Upvotes: 0
Reputation: 14468
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
Upvotes: 3
Reputation: 89
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
Upvotes: 2
Reputation: 7669
# 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.
Upvotes: 16
Reputation: 306
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
Upvotes: 130
Reputation: 11
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.
Upvotes: 2
Reputation: 113
(WARNING: IT WILL DELETE ALL YOUR DATABASES)
Stop the postgres service:
brew services stop postgres
Delete all files in "postgres" directory:
rm -rf /usr/local/var/postgres/*
Initialize the new database system:
initdb /usr/local/var/postgres -E utf8
Start postgres:
pg_ctl -D /usr/local/var/postgres -l server.log start
Upvotes: -1
Reputation: 1379
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.
Upvotes: 14
Reputation: 177
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:
$PATH
using terminal: sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp
pgAdmin
GUI which is about ~100 MB from: https://www.pgadmin.org/download/pgadmin-4-macos/Upvotes: 0
Reputation: 11
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
.
Upvotes: 0
Reputation: 1
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
Upvotes: 7
Reputation: 1172
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
Upvotes: 4
Reputation: 13
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/
Upvotes: 5
Reputation: 151
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
Upvotes: 3
Reputation: 158
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
Upvotes: 14
Reputation: 704
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.
Upvotes: 0
Reputation: 575
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.
Upvotes: 0
Reputation: 8021
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/
Upvotes: 1
Reputation: 886
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.
Upvotes: 2
Reputation: 11
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.
Upvotes: 2
Reputation: 7307
$ 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.
Upvotes: 1
Reputation: 3476
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.
Upvotes: 0
Reputation: 1024
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.
Upvotes: 8
Reputation: 8731
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.
Upvotes: 33
Reputation: 16440
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
Upvotes: 7
Reputation: 601
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
Upvotes: 522
Reputation: 479
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.
Upvotes: 10
Reputation: 11
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
Upvotes: 5
Reputation: 145
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.
Upvotes: 29