Postgres on Ubuntu, controlling the postgresql.conf location - postgresql

I created a cluster as follows:
pg_createcluster -d /some_dir/pg_data/ -p 5432 \
--environment=/some_dir/pg_env.prod.conf \
--createclusterconf=/some_dir/pg.prod.conf \
13 apif
However, when the cluster is stood-up, it has effectively copied the pg.prod.conf file to the default directory (-c config_file=/etc/postgresql/13/apif/postgresql.conf) rather than using it. That means if I make changes to the file, of course, they won't be seen. Thats not what I wanted.
sudo systemctl status postgresql#13-apif
● postgresql#13-apif.service - PostgreSQL Cluster 13-apif
Loaded: loaded (/lib/systemd/system/postgresql#.service; indirect; vendor preset: enabled)
Active: active (running) since Wed 2021-12-15 21:15:25 UTC; 6s ago
Process: 6791 ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast 13-apif stop (code=exited, status=2)
Process: 6656 ExecReload=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 13-apif reload (code=exited, status=0/SUCCESS)
Process: 6868 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 13-apif start (code=exited, status=0/SUCCESS)
Main PID: 6873 (postgres)
Tasks: 9 (limit: 4915)
CGroup: /system.slice/system-postgresql.slice/postgresql#13-apif.service
├─6873 /usr/lib/postgresql/13/bin/postgres -D /some_dir/apif -c config_file=/etc/postgresql/13/apif/postgresql.conf
├─6874 postgres: logger
├─6876 postgres: checkpointer
├─6877 postgres: background writer
├─6878 postgres: walwriter
├─6879 postgres: autovacuum launcher
├─6880 postgres: stats collector
├─6881 postgres: pg_cron launcher
└─6882 postgres: logical replication launcher
Dec 15 21:15:22 ip-172-33-2-19 systemd[1]: Starting PostgreSQL Cluster 13-apif...
Dec 15 21:15:25 ip-172-33-2-19 systemd[1]: Started PostgreSQL Cluster 13-apif.
...
How do I do this the way I want to?... where the cluster is configured to reference a file of my choosing, permanently.

I tried to find an answer and couldn't.
But I did find an acceptable work-around. And while this isn't quite what I was looking for because it doesn't explicitly specify the location of the conf file, it does allow me to reference it in a replicable one-liner.
pg_createcluster -d /some_dir/pg_data/ -p 5432 \
--environment=/some_dir/pg_env.prod.conf \
--pgoption include_if_exists=/some_dir/pg.prod.conf \
13 apif

Related

How can I upgrade PostgreSQL from version 11 to version 13?

I'm trying to upgrade PostgreSQL from 11 to 13 on a Debian system, but it fails. I have a single cluster that needs to be upgraded:
$ sudo -u postgres pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
11 main 5432 online postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
Here's what I've tried to upgrade it:
$ sudo -u postgres pg_upgradecluster 11 main
Stopping old cluster...
Warning: stopping the cluster using pg_ctlcluster will mark the systemd unit as failed. Consider using systemctl:
sudo systemctl stop postgresql#11-main
Restarting old cluster with restricted connections...
Notice: extra pg_ctl/postgres options given, bypassing systemctl for start operation
Error: cluster configuration already exists
Error: Could not create target cluster
After this, the system is left in an unusable state:
$ sudo systemctl status postgresql#11-main.service
● postgresql#11-main.service - PostgreSQL Cluster 11-main
Loaded: loaded (/lib/systemd/system/postgresql#.service; enabled-runtime; vendor preset: enabled)
Active: failed (Result: exit-code) since Tue 2022-06-14 06:48:20 CEST; 19s ago
Process: 597 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 11-main start (code=exited, status=0/SUCCE>
Process: 4508 ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast 11-main stop (code=exited, status=>
Main PID: 684 (code=exited, status=0/SUCCESS)
CPU: 1.862s
Jun 14 06:47:23 argos systemd[1]: Starting PostgreSQL Cluster 11-main...
Jun 14 06:47:27 argos systemd[1]: Started PostgreSQL Cluster 11-main.
Jun 14 06:48:20 argos postgresql#11-main[4508]: Cluster is not running.
Jun 14 06:48:20 argos systemd[1]: postgresql#11-main.service: Control process exited, code=exited, status=2/INVALIDARG>
Jun 14 06:48:20 argos systemd[1]: postgresql#11-main.service: Failed with result 'exit-code'.
Jun 14 06:48:20 argos systemd[1]: postgresql#11-main.service: Consumed 1.862s CPU time.
$ sudo systemctl start postgresql#11-main.service
Job for postgresql#11-main.service failed because the service did not take the steps required by its unit configuration.
See "systemctl status postgresql#11-main.service" and "journalctl -xe" for details.
Luckily, rebooting the system brought the old cluster back online, but nothing has been upgraded. Why does the upgrade fail? What are "the steps required by its unit configuration"? How can I upgrade PostgreSQL with minimal downtime?
I found the source of my problem: a configuration file owned by the wrong user (root instead of postgres) that could not be removed by the pg_dropcluster command because I ran it as the user postgres.
For future reference, here are the correct steps to upgrade a PostgreSQL cluster from 11 to 13:
Verify the current cluster is the still the old version:
$ pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
11 main 5432 online postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
13 main 5434 down postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log
Run pg_dropcluster 13 main as user postgres:
$ sudo -u postgres pg_dropcluster 13 main
Warning: systemd was not informed about the removed cluster yet.
Operations like "service postgresql start" might fail. To fix, run:
sudo systemctl daemon-reload
Run the pg_upgradecluster command as user postgres:
$ sudo -u postgres pg_upgradecluster 11 main
Verify that everything works, and that the only online cluster is now 13:
$ pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
11 main 5434 down postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
13 main 5432 online postgres /var/lib/postgresql/13/main /var/log/postgresql/postgresql-13-main.log
Drop the old cluster:
$ sudo -u postgres pg_dropcluster 11 main
Uninstall the previous version of PostgreSQL:
$ sudo apt remove 'postgresql*11'
The Debian packages create a cluster automatically when you install the server package, so get rid of that:
pg_dropcluster 13 main
Then stop the v11 server and try again.

systemctl status postgresql.service showing output as differently

Not coming out of os(Linux) prompt.Need to do ctrl+c to come out from prompt
output is not showing as expected(Active: activating (start) instead of Active: active (running))
but postgresql services are starting.
# systemctl status postgresql.service
● postgresql.service - PostgreSQL database server
Loaded: loaded (/etc/systemd/system/postgresql.service; enabled; vendor preset: disabled)
Active: activating (start) since Thu 2021-02-04 12:38:17 CET; 15min ago
Docs: man:postgres(1)
Main PID: 6688 (postgres)
CGroup: /system.slice/postgresql.service
├─6688 /opt/data/pgsql/10.15/bin/postgres -D /opt/data/postgres/data/10/data
├─6689 postgres: logger process
├─6696 postgres: checkpointer process
cat /etc/systemd/system/postgresql.service
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
[Service]
Type=notify
User=postgres
ExecStart=/opt/data/pgsql/10.15/bin/postgres -D /opt/data/postgres/data/10/data
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0
[Install]
WantedBy=multi-user.target
So please can any one help me to get desired output as Active: active (running))
I tried comment the Type=Notify line and it works fine.
Only for PostgreSQL 9x.
Version 10 and above I've no problem.

Updated Xubuntu, Postgres Stopped Working

I updated my Xubunutu, It was originally 19.04 when I install on this labtop, I think I did one update in the past, now on Xubuntu 19.10.
Postgres was working just prior to the upgrade, I think I may have two versions of postgres running but many of the solutions I've seen don't seem to work.
When I type: service postgresql status
I get
postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor prese
Active: active (exited) since Mon 2019-12-09 07:33:43 EST; 4h 30min ago
Main PID: 1217 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 4915)
Memory: 0B
CGroup: /system.slice/postgresql.service
Dec 09 07:33:43 alexmerced-X550EA systemd[1]: Starting PostgreSQL RDBMS...
Dec 09 07:33:43 alexmerced-X550EA systemd[1]: Started PostgreSQL RDBMS.
lines 1-10/10 (END)
when I type: dpkg -l | grep postgres
I get:
ii postgresql 11+204ubuntu0.1 all object-relational SQL database (supported version)
ii postgresql-11 11.5-1 amd64 object-relational SQL database, version 11 server
ii postgresql-client-11 11.5-1 amd64 front-end programs for PostgreSQL 11
ii postgresql-client-common 204ubuntu0.1 all manager for multiple PostgreSQL client versions
ii postgresql-common 204ubuntu0.1 all PostgreSQL database-cluster manager
When I type: pg_lsclusters
I get:
pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
11 main 5432 down postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
Just in case: psql -h /tmp
psql -h /tmp
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"?
sudo service postgres restart
Failed to restart postgres.service: Unit postgres.service not found.
ps aux |grep postgres
alexmer+ 26272 0.0 0.0 9028 916 pts/2 S+ 12:11 0:00 grep --color=auto postgres
The fix came in by uninstall all the packages from the dpkg results in the OP, then reinstalling with sudo apt-get install postgresql.
now reconfiguring my users

postgresql could not properly start exited with active (exited)

I installed posgresql from digitalocean and in the end of installation prints the below command in terminal
/usr/lib/postgresql/10/bin/pg_ctl -D /var/lib/postgresql/10/main -l logfile start
I tried to run it with sudo root user and also with switching to postgres user but gives me below error
waiting for server to start..../bin/sh: 1: cannot create logfile:
Permission denied stopped waiting pg_ctl: could not start server
but when i check the status it says
● postgresql.service - PostgreSQL RDBMS Loaded: loaded
(/lib/systemd/system/postgresql.service; enabled; vendor preset:
enabled) Active: active (exited) since Thu 2018-05-31 13:11:18 UTC;
56s ago Main PID: 3698 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 2362) CGroup: /system.slice/postgresql.service
May 31 13:11:18 staging systemd1: Starting PostgreSQL RDBMS... May
31 13:11:18 staging systemd1: Started PostgreSQL RDBMS.
Status is not running except is exited.what the above command do and how can i run it ? I haven't faced it in previous versions
The idea is that you supply your actual log file instead of logfile, but I recommend that you configure logging properly in postgresql.conf and use pg_ctl without the -l option.
Set logging_collector to on.
Set log_filename to postgresql-%a.log.
Set log_rotation_size to 0.
Set log_truncate_on_rotation to on.
Then you'll get the log files in the log subdirectory of your PostgreSQL data directory, and they will be rotated on a weekly basis.

'service postgresql start' fails to start postgres service on Fedora

Newcomer to postgres here!
I edited pg_hba.conf as mentioned here , but when I try to restart postgresql service, the attempt fails. Below is the command line output with all the information I could gather.
[root#arunpc modules]# service postgresql restart
Redirecting to /bin/systemctl restart postgresql.service
Job failed. See system logs and 'systemctl status' for details.
[root#arunpc modules]# systemctl status postgresql.service
postgresql.service - PostgreSQL database server
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled)
Active: failed since Sun, 08 Apr 2012 21:29:06 +0530; 14s ago
Process: 12228 ExecStop=/usr/bin/pg_ctl stop -D ${PGDATA} -s -m fast (code=exited, status=0/SUCCESS)
Process: 12677 ExecStart=/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=1/FAILURE)
Process: 12672 ExecStartPre=/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
Main PID: 12184 (code=exited, status=0/SUCCESS)
CGroup: name=systemd:/system/postgresql.service
[root#arunpc modules]# tail /var/log/messages
....
Apr 8 21:29:06 arunpc systemd[1]: postgresql.service: control process exited, code=exited status=1
Apr 8 21:29:06 arunpc systemd[1]: Unit postgresql.service entered failed state.
Apr 8 21:29:06 arunpc pg_ctl[12677]: pg_ctl: could not start server
Apr 8 21:29:06 arunpc pg_ctl[12677]: Examine the log output.
FWIW, here is the configuration file (pg_hba.conf) used:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all postgres ident sameuser
local all all ident sameuser
# IPv4 local connections:
host all all 127.0.0.1 password
# IPv6 local connections:
host all all ::1 password
What could be the error here? It used to work fine before I made the edit (and since this was a development machine, I brilliantly didn't make any backup).
I would also like to get a more detailed log output. The log message in /var/log/messages file does ask me to "Examine the log output" - which log output would this be? What other troubleshooting steps can I take?
Many thanks in advance!
Depending on your startup script, it might redirect the postmaster's output to a file. This is usually server.log in the PGDATA directory. Things I'd try:
Comment out everything in pg_hba.conf and retry. If the problem is a syntax error in that file, then commenting out the offending line will allow the server to start and then you'll be able to uncomment one at a time until you find the error.
Start postmaster directly from the shell without sending it to the background. Just run postmaster -D <pgdata dir> and it should spew some more helpful logs.