Issue in restarting postgres service - postgresql

I have updated the postgres version to 9.5 (using yum update)
When I try to see the postgres version by :
postgres --version
postgres (PostgreSQL) 9.2.23
There is postgres service running. I confirmed ut by :
systemctl list-unit-files | grep postg
postgresql.service enabled
When I try to stop the running postgres service by :
/etc/init.d/postgresql.service stop
bash: /etc/init.d/postgresql.service: No such file or directory
I tried to start the postgres service gave me same No such file or directory issue.
When I tried to dig into the logs, I found something as below
postgresql.service: control process exited, code=exited status=1
Feb 07 16:06:10 punessgdev2 systemd[1]: Failed to start PostgreSQL database
server.
How do I proceed? Any kind of help is appreciated.

$ sudo systemctl status/start/restart postgresql.service
work fine to me DEBIAN 9 POSTGRES 9.6.15

Related

Not able to start PostgreSQL

Have a CentOS7 Linux machine running (not managed by me; have limited rights to/in it).
Have a request to set PostgreSQL up in it.
Just installed PostgreSQL from the CentOS repository:
sudo yum install postgresql-server postgresql-contrib
All good with it.
Then did initialization of the database:
sudo yum install postgresql-server postgresql-contrib
All good with it.
But then the start:
sudo systemctl start postgresql
Fails:
Job for postgresql.service failed because the control process exited with error code. See "systemctl status postgresql.service" and "journalctl -xe" for details.
I do not have access to systemd-journal so nothing tod with "journalctl -xe", but then the:
systemctl status postgresql.service
returns:
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2020-04-24 16:41:04 EEST; 33s ago
Process: 30690 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=1/FAILURE)
Process: 30684 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
postgres --version
postgres (PostgreSQL) 9.2.24
Any ideas what could be wrong here? Hot to continue with that?
As the echo $PGDATA shows you have PG9.3 as well. Which means that 9.2 service will not start because the port 5432 might be already occupied by postgres 9.3
Do the following
ps- ef | grep postgres
And get the postgres process id and kill all postgres processes.
Remove the 9.3 data directory using
rm -rf /var/lib/pgsql/9.3
Go to /usr/pgsql-9.2/bin and run the initdb using below command
./postgresql92-setup initdb
After that start the postgresql-9.2 service using
systemctl start postgresql-9.2.service
There must be both PostgreSQL 9.2 and 9.3 installed on your machine. Deinstall them both and install version 12 from the PostgreSQL site. Remove the data directory and create a new one with the v12 installation. Then try again.

How to explicitly start one version of postgres on startup?

I manage an old ubuntu server 12.04 that has postgresql 9.1 on it.
I successfully installed postgresql 9.6 for a new application that we want to use.
The problem I'm facing is for some reason
service postgresql start
* Starting PostgreSQL 9.1 database server
[ OK ]
* Starting PostgreSQL 9.6 database server
[ OK ]
starts the 9.1 server and the 9.6 server.
I'm looking for a way to only start the 9.6 server on the default port.
I've looked at /etc/inid.d/postgresql.
This extract looks like it might be the key.
# versions can be specified explicitly
if [ -n "$2" ]; then
versions="$2 $3 $4 $5 $6 $7 $8 $9"
else
get_versions
fi
looks like I just have to pass in the version on the command line.
service postgresql start 9.6
and yes ... it worked.
So my question is ... how do I make this automatically happen in the instance of a reboot?
If you have multiple instance of postgresql, you can start just one service by following command:
systemctl start postgresql#<version>-main
For example, I have version 11, 12 & 13 installed in my PC. I can start version 13 only by:
systemctl start postgresql#13-main
OR using service command
service postgresql#13-main start
One solution is to remove 9.1
apt-get --purge remove postgresql-9.1
and change the port in the config file for 9.6 to 5432
su postgres
psql
psql (10.3 (Ubuntu 10.3-1.pgdg14.04+1), server 9.6.8)
Type "help" for help.
postgres=#
SHOW config_file;
config_file
------------------------------------------
/etc/postgresql/9.6/main/postgresql.conf
(1 row)
\quit
exit
vi /etc/postgresql/9.6/main/postgresql.conf
/port
change the port to 5432 and write and close the config file
:wq
service postgresql stop
service postgresql start

PostgreSQL Services

Can someone give a detailed explanation of how the postgresql service(s) works on Ubuntu [16.04]? The behavior that leads me to ask is the following. I use the command sudo systemctl start postgresql to start my postgresql service. When I run systemctl list-units | grep post I see
UNIT LOAD ACTIVE SUB DESCRIPTION
postgresql.service loaded active exited PostgreSQL RDBMS
postgresql#9.6-main.service loaded active running PostgreSQL Cluster 9.6-main
system-postgresql.slice loaded active active system-postgresql.slice
and when I run sudo systemctl stop postgresql both postgresql.service and postgresql#9.6-main.service disappear. What is each of these three services doing?
I assume postgresql#9.6-main.service is registered service for PostgreSQL Cluster 9.6-main cluster. This is the one that actually controls postgres on your system.
I assume postgresql.service is the effect desribed here
I assume system-postgresql.slice is a postgres slice unit
none of above are critical for running postgres. You can start/stop the cluser with just pg_ctl, without registering it with your services. But if you wonder which service runs it in your case - then it is postgresql#9.6-main.service

Systemd cannot start Postgresql on Red Hat EL7

On Red Hat EL7, I installed Postgres from the standard repository and initialized the database in the usual way:
# postgresql-setup initdb
But when I try to start the service with:
# systemctl start postgresql.service
I get an error:
Job for postgresql.service failed. See 'systemctl status postgresql.service' and 'journalctl -xn' for details.
journalctl -xn gives me:
Oct 06 14:52:55 myserver systemd[1]: Starting PostgreSQL database server...
-- Subject: Unit postgresql.service has begun with start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit postgresql.service has begun starting up.
Oct 06 14:52:55 myserver systemd[29267]: Failed at step USER spawning /usr/bin/postgresql-check-db-dir: No such process
-- Subject: Process /usr/bin/postgresql-check-db-dir could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- The process /usr/bin/postgresql-check-db-dir could not be executed and failed.
--
-- The error number returned while executing this process is 3.
Anyone have any ideas?
I don't consider this a complete and proper fix, but it's what I'd call a "strong workaround". The problem seems to start with the fact that this Linux system can authenticate locally as well as through a directory server. The PostgreSQL package tries to create a user:group combo called postgres:postgres. The user "postgres" wasn't created because it already exists in the directory server. The group "postgres" was created locally.
When systemd starts Postgres, it changes into the postgres user before trying to call postgresql-check-db-dir which is a script that checks whether the database directory has been intialized with postgresql-setup initdb. It seems that systemd can't change to this directory-supplied postgres user.
My work-around was to create a new local user ("pgsql") and change the ownership of /var/lib/pgsql and /var/run/postgresql to this new user, then create a custom systemd service file called /etc/systemd/system/postgresql.service to override the default one. The contents of this file are:
.include /usr/lib/systemd/system/postgresql.service
[Service]
User=pgsql
Group=postgres
That allows Postgres to be started and stopped by systemd.
For me the logs are telling you that the db path does not exist. Are you sure psql is configured correctly, the db path exists and you have permission to access it ?

Error upgrading postgresql cluster from 9.1 to 9.2

I'm on Ubuntu 12.04 64 bit on Amazon EC2.
Trying to upgrade postgresql from 9.1 to 9.2.
$ uname -a
Linux db2 3.2.0-32-virtual #51-Ubuntu SMP Wed Sep 26 21:53:42 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
$ apt-cache policy postgresql
postgresql:
Installed: 9.1+136~precise
Candidate: 9.1+136~precise
Version table:
*** 9.1+136~precise 0
500 http://ppa.launchpad.net/pitti/postgresql/ubuntu/ precise/main amd64 Packages
100 /var/lib/dpkg/status
9.1+129ubuntu1 0
500 http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ precise-updates/main amd64 Packages
9.1+129 0
500 http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ precise/main amd64 Packages
The upgrade process I'm following is:
$ sudo add-apt-repository ppa:pitti/postgresql
$ sudo apt-get update
$ sudo apt-get install postgres-9.2
$ sudo pg_dropcluster --stop 9.2 main
$ sudo pg_upgradecluster 9.1 main /var/lib/postgresql/9.2
Stopping old cluster...
Disabling connections to the old cluster during upgrade...
Restarting old cluster with restricted connections...
Creating new cluster (configuration: /etc/postgresql/9.2/main, data: /var/lib/postgresql/9.2)...
Moving configuration file /var/lib/postgresql/9.2/postgresql.conf to /etc/postgresql/9.2/main...
Moving configuration file /var/lib/postgresql/9.2/pg_hba.conf to /etc/postgresql/9.2/main...
Moving configuration file /var/lib/postgresql/9.2/pg_ident.conf to /etc/postgresql/9.2/main...
Configuring postgresql.conf to use port 5433...
Disabling connections to the new cluster during upgrade...
Roles, databases, schemas, ACLs...
Fixing hardcoded library paths for stored procedures...
ERROR: cannot set transaction read-write mode during recovery
Error: Could not fix library paths
Re-enabling connections to the old cluster...
Re-enabling connections to the new cluster...
Error during cluster dumping, removing new cluster
Any help would be appreciated.
Thank you.
The root cause of your issue is that hot_standby is on in postgresql.conf, so the server is read-only.
In general, if you're having issues with pg_upgradecluster from the pg_wrapper tools typically packaged in Debian and Ubuntu you can do a manual cluster upgrade instead:
Start the old server
sudo -i -u postgres
for db in $(psql --tuples-only template1 -c "select datname from pg_database where datname not in ('template0','template1','postgres','template_postgis');"); do pg_dump -Fc -f $db.backup $db; done
pg_dumpall --globals-only > globals.sql
Stop the old server
initdb a new cluster on the new server if you've removed it. With pg_wrapper I think you use pg_createcluster for this.
Start the new server; and still as the postgres user:
psql -f globals.sql
for backup in *.backup; do pg_restore --dbname postgres --create $backup; done
Alternately, use the pg_upgrade tool tool to in-place convert your DB, but that might confuse pg_wrapper.
These steps can be simplified by using the pg_dumpall command to make a whole cluster dump, but I don't like it much. I think restoring pg_dumpall dumps leaves much to be desired in terms of error handling, it's hard to extract individual DBs or tables from the dump, and it can't all be restored in a single transaction. I strongly prefer using pg_dumpall only for globals like users/groups/roles, and pg_dump per-database custom-format backups for individual databases as shown above.
General steps for upgrading if you installed from apt.
In my case from pg11 to pg12:
sudo apt update
sudo apt install postgresql-12
sudo systemctl stop postgresql#12-main #stop the autocreated/autostarted one
pg_dropcluster 12 main # drop it
sudo -u postgres pg_upgradecluster -v 12 11 main
sudo systemctl start postgresql#12-main
sudo -u postgres psql <yourdb> # up and test it