Kenshō - PostgresQL and the catastrophy of not stopping the service when time.

Prerequisites

Installing postresQL

First, things first - having the dear database tech in your system and having it started.

brew install postgres python3
brew services start postgresql

Start, stop, restart, reload the service

Starting postgresQL service is crucial before serving data on backend and is a safe move to stop the service when the work is done. service command restart carries out two tasks, that is, it first stops a postgres service and then starts it again. This process of restart is different from the process of reload in the sence that:

$ brew services stop postgres
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)

$ brew services start postgres
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

$ brew services start postgresql
Service `postgresql` already started, use `brew services restart postgresql` to restart.

$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

$ brew services reload postgres
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

Checking ports

$ fab serve
$ python manage.py runserver 127.0.0.1:8000
$ python manage.py runserver 192.168.1.6:8000
$ fab serve --port 8002
$ fab serve --port 8080

Mac restarted itself. The following commands were yielding the same error as shown below. Initially, the error seemed to be a cause of 8000 port being blocked by firewall. On checking, there wasn’t any firewall set up. So, on further checks it was made clear that it was a PostgresQL error caused due to the service being interrupted by abrupt shutting down of the system.

  File "/Users/tanyaacjain/Documents/gitlab/life-force-fitness/milife-backend/venv/lib/python3.7/site-packages/psycopg2/__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: 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?
could not connect to server: Connection refused
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?

Tried Solutions

$ brew services start postgresql
Service `postgresql` already started, use `brew services restart postgresql` to restart.
$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start.... stopped waiting
pg_ctl: could not start server
Examine the log output.
$ ps auxwww | grep postgres
tanyaacjain       3317   0.0  0.0  4268320    684 s008  S+    8:37PM   0:00.00 grep postgres
$ brew info postgres
postgresql: stable 12.3 (bottled), HEAD
Object-relational database system
https://www.postgresql.org/
/usr/local/Cellar/postgresql/12.3_4 (3,220 files, 37.8MB) *
  Poured from bottle on 2020-06-01 at 14:38:07
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/postgresql.rb
==> Dependencies
Build: pkg-config ✔
Required: icu4c ✔, krb5 ✔, openssl@1.1 ✔, readline ✔
==> Options
--HEAD
        Install HEAD version
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start
==> Analytics
install: 206,947 (30 days), 450,159 (90 days), 1,149,389 (365 days)
install-on-request: 197,418 (30 days), 429,916 (90 days), 1,074,714 (365 days)
build-error: 0 (30 days)
$ pg_ctl -D /usr/local/var/postgres -l logfile start
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start.... stopped waiting
pg_ctl: could not start server
Examine the log output.
$ postgres -D /usr/local/var/postgres
2020-06-09 20:39:44.773 IST [3544] FATAL:  lock file "postmaster.pid" already exists
2020-06-09 20:39:44.773 IST [3544] HINT:  Is another postmaster (PID 869) running in data directory "/usr/local/var/postgres"?
$ pg_isready
/tmp:5432 - no response
$ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents 
/Users/tanyaacjain/Library/LaunchAgents/homebrew.mxcl.postgresql.plist -> /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
/Users/tanyaacjain/Library/LaunchAgents/homebrew.mxcl.postgresql.plist: service already loaded
$ egrep 'listen|port' /usr/local/var/postgres/postgresql.conf
#listen_addresses = 'localhost'                # what IP address(es) to listen on;
#port = 5432                                   # (change requires restart)
#ssl_passphrase_command_supports_reload = off
                                        # supported by the operating system:
                                        # supported by the operating system:
                                        # supported by the operating system:
                                        #   %r = remote host and port

Solution that worked

$ brew services stop postgres
$ rm /usr/local/var/postgres/postmaster.pid
$ pg_ctl -D /usr/local/var/postgres start
waiting for server to start....2020-06-09 20:58:10.115 IST [3852] LOG:  starting PostgreSQL 12.3 on x86_64-apple-darwin19.4.0, compiled by Apple clang version 11.0.3 (clang-1103.0.32.59), 64-bit
2020-06-09 20:58:10.116 IST [3852] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-06-09 20:58:10.117 IST [3852] LOG:  listening on IPv6 address "::1", port 5432
2020-06-09 20:58:10.118 IST [3852] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-06-09 20:58:10.143 IST [3853] LOG:  database system was interrupted; last known up at 2020-06-09 17:49:34 IST
2020-06-09 20:58:10.266 IST [3853] LOG:  database system was not properly shut down; automatic recovery in progress
2020-06-09 20:58:10.271 IST [3853] LOG:  redo starts at 0/19A5A50
2020-06-09 20:58:10.271 IST [3853] LOG:  invalid record length at 0/19A5B38: wanted 24, got 0
2020-06-09 20:58:10.271 IST [3853] LOG:  redo done at 0/19A5B00
2020-06-09 20:58:10.280 IST [3852] LOG:  database system is ready to accept connections
 done
server started