13

When I try to run a server:

postgres@ubuntu:~$ /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
LOG:  could not bind IPv4 socket: Address already in use
HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
WARNING:  could not create listen socket for "localhost"
FATAL:  could not create any TCP/IP sockets postgres@ubuntu:~$ 

Then I change user to myself:

postgres@ubuntu:~$ su - michael

michael@ubuntu:~$  sudo netstat -tulpn | grep 5432
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      959/postgres 

Well, postgres seems to be listening to the port and this seems to be the problem.

Let us change pack to postgres and try to kill this process:

postgres@ubuntu:~$ kill `cat /usr/local/pgsql/data/postmaster.pid`

The reaction is:

cat: /usr/local/pgsql/data/postmaster.pid: No such file or directory
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

Could you recommend me how to proceed?

Kifsif
  • 3,477
  • 10
  • 36
  • 45

1 Answers1

14

First, get the running Postgres pid:

ps -ef | grep postmaster | grep -v grep | awk '{print $2}'

Then kill it

kill <the_pid_you_just_got>

But unless you are trying to do something very special with Postgres (multiple instances...) you should stop it using sudo /etc/init.d/postgresql stop (or sudo /etc/init.d/postgres stop) AND start it using sudo /etc/init.d/postgresql start

Postgres runs as a service, and so it has a service control file/script which is in charge of correctly starting and stopping it. These control files used to be located inside /etc/init.d, but I must admit it has become a bit messy these days, with the growing number of services management systems (init, upstart, systemd...)

Krishna
  • 6,107
  • 2
  • 40
  • 43
mbarthelemy
  • 12,465
  • 4
  • 41
  • 43
  • Well, I tried ps -ef | grep postmaster | awk '{print $1}'. Under the user michael it showed michael. Well, I stopped Postgres as you suggested. Now there is nothing to listen to the port. I could not catch your remark about start. So, I deleted the data directory. Then I started from /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data. The reaction was: creating directory /usr/local/pgsql/data ... initdb: could not create directory "/usr/local/pgsql/data": Permission denied. Earlier I created data directory manually through sudo. Now: /usr/local$ ls -l results in – Kifsif Oct 05 '12 at 13:36
  • drwxr-xr-x 6 root root 4096 Oct 5 17:25 pgsql. Maybe I should just start everything from the very beginning and use another directory rather than /usr/local? – Kifsif Oct 05 '12 at 13:37
  • If you created 'data' yourself with sudo, it belongs to 'root', not to you. You can change that by taking ownership of this folder : sudo chown michael. /usr/local/pgsql/data , but Are you sure you want to run, create and manage your databases under the michael account? – mbarthelemy Oct 05 '12 at 13:43
  • Well, no, I'm too green to derive from the manual. I would like just to create my first database. I already deleted data directory and would like initdb to create it again. But it somehow couldn't do that because of permission privileges. – Kifsif Oct 05 '12 at 13:51
  • 11
    When I ran this command, I got 502 . so I then did the command `kill 502` but then got the error `-bash: kill: (502) - No such process` – JGallardo Aug 23 '13 at 01:16
  • 4
    I get a different ps id everytime i run the fist command – Joey Gough Jan 03 '21 at 19:19
  • The first command prints 2 pids: postmaster pid itself and the running `grep postmaster` pid because it contains `postmaster` in arguments... – Enbugger Nov 18 '21 at 17:21
  • 1
    The suggested line is wrong and will show the grep process itself. A correct version is `ps -ef | grep postmaster | grep -v grep | awk '{print $2}'`. If this is blank, there's nothing running called postmaster. – MikeTwo Feb 11 '22 at 17:21