Postgres won't start after reboot on Linux - postgresql

I am running postgres 12 and it won't start after server reboot (run manually by reboot). The attached EBS volume has not changed and I don't see any evidence of a data loss.
When I run psql, I get (this used to work before restart)
psql -h localhost -U postgres
psql: error: 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?
pg_lsclusters does not help -
pg_lsclusters 12 main start
Ver Cluster Port Status Owner Data directory Log file
12 main 5432 down postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
The error in /var/log/postgresql/postgresql-12-main.log is
pg_ctl: could not start server
Examine the log output.
2021-03-01 00:33:24.976 UTC [1866] FATAL: could not access file "anon": No such file or directory
2021-03-01 00:33:24.976 UTC [1866] LOG: database system is shut down
When I start postgres, I don't get any error -
sudo service postgresql start

If you don’t want to start PostgreSQL manually every time your Linux restarts, run the following command :
sudo update-rc.d postgresql enable
If it can not find LSB script then do this :
sudo systemctl enable postgresql

Related

Postgresql not starting even after editing rc file

I receive the following error when I try to start postgresql aka psql in Slackware Linux:
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
I am trying to start it and changed the field in pg_hba.conf to trust from md5.
At some point it was asking for a password.
I get the following when I try to start the server:
bash-5.1# postgres -D /pgsql/data
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise. See the documentation for
more information on how to properly start the server.
bash-5.1# postgres -D /pgsql/data >logfile 2>&1 &
[1] 8310

pgAdmin: could not connect to server: Connection refused

I am trying to view databases with pgAdmin. After starting pgAdmin and successfully entering my "master password". I get the following error while trying to open up the only instance, in this case: "PostgeSQL 12".
I am getting this pgAdmin error:
could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? 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?
Picture of: Connection refused error
After entering the correct and only password I have used for 4 or so different programs: I get the same pop-up error. None of the programs access the database any longer.
I did try right-clicking "PostgreSQL 12" and clearing the password but that did not resolve the issue.
I will say that I recently restored from a backup because I did a hardware upgrade where I swapped out a HDD to a SSD.
I am running the following:
MacOS High Sierra 10.13.6
PostgreSQL 12
pgAdmin 4
UDATE 1:
I found something: I think this is the installation full path: /Library/PostgreSQL/12/bin
I ran from bin:
./pg_ctl --help
and got the full help menu options. Would I now have to run status, start, or stop on the correct server? Which server or file might that be? Is it data, which seems to be locked?
UPDATE 2:
I ran ./pg_ctl start
and got: "pg_ctl: no database directory specified and environment variable PGDATA unset"
UPDATE 3:
I ran ./pg_ctl start -D /Library/PostgreSQL/12/data
and got: "pg_ctl: could not open PID file "/Library/PostgreSQL/12/data/postmaster.pid": Permission denied"
Picture of: Directory
UPDATE 4:
ran from the bin folder found at /Library/PostgreSQL/12/bin I ran:
sudo -u postgres ./pg_ctl start -D /Library/PostgreSQL/12/data
and after inputing password:
waiting for server to start....2020-04-22 15:57:51.766 CDT [5255] LOG: starting PostgreSQL 12.2 on x86_64-apple-darwin, compiled by Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn), 64-bit
2020-04-22 15:57:51.766 CDT [5255] LOG: listening on IPv6 address "::", port 5432
2020-04-22 15:57:51.766 CDT [5255] LOG: listening on IPv4 address "0.0.0.0", port 5432
2020-04-22 15:57:51.768 CDT [5255] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-04-22 15:57:51.782 CDT [5255] LOG: redirecting log output to logging collector process
2020-04-22 15:57:51.782 CDT [5255] HINT: Future log output will appear in directory "log".
stopped waiting
pg_ctl: could not start server
So, to answer some questions, I could not start the server; hence, the error I am getting in pgAdmin.
If you are on windows, You need to start the Postgres in the task manager services section. That way It would connect automatically and ensure there is no firewall connection block on your port 5432. I tried it, and it worked.

Installing PostgreSQL and `createdb` with Terminal

After installing PostgreSQL from the terminal with Homebrew...
➜ ~ brew link postgresql
Warning: Already linked: /usr/local/Cellar/postgresql/11.2_1: 3,186 files, 35.3MB
To relink: brew unlink postgresql && brew link postgresql
➜ ~ brew services restart postgresql
Successfully stopped postgresql (label: homebrew.mxcl.postgresql)
Successfully started postgresql (label: homebrew.mxcl.postgresql)
➜ ~ createdb 'test'
createdb: could not connect to database template1: could not connect to server: No server file or directory
Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
I would like to be able to run strictly from terminal and not be subject to using the PSequel GUI...
Thanks,
Solved
My main issue was:
➜ ~ createdb 'test'
createdb: could not connect to database template1: could not connect to server: No server file or directory
Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
this issue was simply solved by:
➜ ~ postgres -D /usr/local/var/postgres
which invocated the database directory...
2019-05-06 14:20:37.367 EDT [41854] LOG: listening on IPv6 address "::1", port 5432
2019-05-06 14:20:37.367 EDT [41854] LOG: listening on IPv4 address "127.0.0.1", port 5432
2019-05-06 14:20:37.367 EDT [41854] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-05-06 14:20:37.384 EDT [41854] LOG: database system is ready to accept connections
keeping that running, and opening a new-tab on my terminal:
➜ ~ createdb 'test'
➜ ~ psql 'test'
psql (11.2)
Type "help" for help.
test=#
Something else of use during my debugging:
remove old database files (this is dangerous)
➜ ~ rm -rf /usr/local/var/postgres
followed-up with
➜ ~ initdb /usr/local/var/postgres
initdb is not used to create a "new database"
As Documented in the Manual you need it to create a "cluster" or "data directory" which then stores databases created with createdb
Quote from the manual:
Before you can do anything, you must initialize a database storage area on disk. We call this a database cluster. (The SQL standard uses the term catalog cluster.) A database cluster is a collection of databases that is managed by a single instance of a running database server
[...]
In file system terms, a database cluster is a single directory under which all data will be stored. We call this the data directory or data area
In short: initdb creates the necessary directory layout on the harddisk to be able to create and manage databases.
It's a necessary part of the installation process of a Postgres server.

Postgresql server is running on local machine but psql can not connect

After doing brew install postgres:
brew install postgres
brew link --overwrite postgresql
Postgres is already running:
/~ $brew services start postgresql
Service `postgresql` already started, use `brew services restart postgresql` to restart.
But then how do we connect using psql client?
13:47:28/~ $psql
psql: 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"?
Updates Tried various suggestions.
Try restarting ..
$brew services start postgresql
Service `postgresql` already started, use `brew services restart postgresql` to restart.
14:37:19/fastparquet $brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
14:37:39/fastparquet $psql
psql: 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"?
14:37:55/fastparquet $
Connect to localhost?
14:37:34/fastparquet $psql -h localhost
psql: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
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?
The link from a_horse_with_no_name did the trick. Specifically:
initdb -D /usr/local/var/postgres/
(from Homebrew postgres broken )
and then
createdb
(the latter from psql: FATAL: database "<user>" does not exist )

Postgresql starting failed

I was connected with remote postgres-9.3 by pgAdmin III. After close pgAdmin i try to connect on another day with the same db.
/etc/init.d/postgresql-9.3 status
dead but pid file exists
service postgresql-9.3 start FAIL
In pgstartup.log file i have:
This account is currently not available
pg_hba.conf:
# "local" is for Unix domain socket connections only
local all all peer
host all all 127.0.0.1/32 trust
host all all 89.70.224.82/32 md5
Operating system on server is CentOS. I don't know unfortunately how postgres was installed on the server because someone else done that.
What i can do with this?
The solution is reset your PostgreSQL logs
[root#user /]#/usr/pgsql-9.3/bin/pg_resetxlog -f /usr/pgsql-9.3/data/
after executing above command display "Transaction log reset"
and then restart PostgreSQL server
[root#user /]# /etc/init.d/postgresql-9.3 restart
Stopping postgresql-9.3 service: [FAILED]
Starting postgresql-9.3 service: [ OK ]
[root#qa /]# /etc/init.d/postgresql-9.3 status
(pid 3003) is running...
after checking your pgadmin
Try to start cluster using native postgres utility.
Assuming you have CentOS 6.6 and postgresql 9.4
sudo -u postgres /usr/pgsql-9.4/bin/pg_ctl -D <YOUR_DATA_DIR> start
You will get some reasonable output.
Then try to start postgres in single user mode
sudo -u postgres /usr/pgsql-9.4/bin/postgres --single -D <YOUR_DATA_DIR> -P -d 1
Beware that resetting xlogs will cause some data lost.
I also recommend to read this article http://blog.endpoint.com/2014/11/when-postgres-will-not-start.html