Restart PostgreSQL without postgresql-server - postgresql

I'm on CentOS 7 and I'm trying to get through the 'PG::ConnectionBad: FATAL: Peer authentication failed for user' error.
So I've already figured out that I should change pg_hba.conf (peer to md5) and I've done it. It seems that I have to restart postgres but it is not so easy as I thought.
I tried 'service postgresql restart' which resulted in 'Failed to restart postgresql.service: Unit not found.'
Then tried to install posgresql-server. Got:
oct 23 01:16:15 serverct1 pg_ctl[3280]: HINT: Is another postmaster already running on port 5432? If ...try.
oct 23 01:16:15 serverct1 pg_ctl[3280]: WARNING: could not create listen socket for "localhost"
oct 23 01:16:15 serverct1 pg_ctl[3280]: FATAL: could not create any TCP/IP sockets
oct 23 01:16:16 serverct1 pg_ctl[3280]: pg_ctl: could not start server
oct 23 01:16:16 serverct1 systemd[1]: postgresql.service: control process exited, code=exited status=1
oct 23 01:16:16 serverct1 systemd[1]: Failed to start PostgreSQL database server.
About 5432 port usage:
postgres 5432/tcp postgresql # POSTGRES
postgres 5432/udp postgresql # POSTGRES
So I'm curious:
1) Do postgresql and postgresql-server work separately?
2) Is it possible to restart posgresql without postgresql-server?
3) If not - how to get the port 5432 free in order to run postgresql-server?

You can avoid troubles with serverct1 if you use standard postgres pg_ctl, eg:
pg_ctl reload
Or if needed pg_ctl reload -D $PGDATA
You dont need to restart the postgres for pg_hba changes to apply: https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html
The pg_hba.conf file is read on start-up and when the main server
process receives a SIGHUP signal. If you edit the file on an active
system, you will need to signal the postmaster (using pg_ctl reload or
kill -HUP) to make it re-read the file.

Related

Unable to connect to server: FATAL: Ident authentication failed for user "postgres"

I got message: Unable to connect to server: FATAL: Ident authentication failed for user "postgres" when I tried to create a connection in pgAdmin.
pgAdmin parameters:
-------------------
Hostname/address: localhost
Port: 5432
Maintenance database: postgres
Username: postgres
Password: postgres
The package is install on a Linux machine. I tried web mode and desktop mode, the problem appeared on both.
Below are the setup steps I follow after installing the package:
# vi /opt/pgdata/pg_hba.conf
host all all all md5
# vi /opt/pgdata/postgresql.conf
listen_addresses = '*'
port = 5432
# postgresql-setup --initdb
* Initializing database in '/var/lib/pgsql/data'
* Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
# cp /opt/pgdata/pg_hba.conf /var/lib/pgsql/pg_hba.conf
# cp /opt/pgdata/postgresql.conf /var/lib/pgsql/postgresql.conf
# systemctl enable postgresql
# systemctl start postgresql
# su - postgres
$ psql postgres
psql (10.6)
Type "help" for help.
postgres=# \password postgres
Enter new password: postgres
Enter it again: postgres
postgres=# \q
# systemctl stop postgresql
# systemctl start postgresql
# systemctl status postgresql
● postgresql.service - PostgreSQL database server
Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
Active: active (running) (thawing) since Tue 2022-07-26 21:23:49 EDT; 28min ago
Process: 40022 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
Main PID: 40025 (postmaster)
Tasks: 8 (limit: 72897)
Memory: 19.3M
CGroup: /system.slice/postgresql.service
├─40025 /usr/bin/postmaster -D /var/lib/pgsql/data
├─40026 postgres: logger process
├─40028 postgres: checkpointer process
├─40029 postgres: writer process
├─40030 postgres: wal writer process
├─40031 postgres: autovacuum launcher process
├─40032 postgres: stats collector process
└─40033 postgres: bgworker: logical replication launcher
Jul 26 21:23:49 localhost.localdomain systemd[1]: Starting PostgreSQL database server...
Jul 26 21:23:49 localhost.localdomain postmaster[40025]: 2022-07-26 21:23:49.641 EDT [40025] LOG: listening on IPv6 address "::1", port 5432
Jul 26 21:23:49 localhost.localdomain postmaster[40025]: 2022-07-26 21:23:49.641 EDT [40025] LOG: listening on IPv4 address "127.0.0.1", port 5432
Jul 26 21:23:49 localhost.localdomain postmaster[40025]: 2022-07-26 21:23:49.642 EDT [40025] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
Jul 26 21:23:49 localhost.localdomain postmaster[40025]: 2022-07-26 21:23:49.645 EDT [40025] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
I can see the status is "active (running) (thawing) since Tue 2022-07-26 21:23:49 EDT; 28min ago"
is that normal?
How to connect to the instance successfully?
Try this
$ sudo psql -U postgres
postgres=# \l
postgres=# \dt
or may be you forget to restart service daemon after change conf
$ sudo systemctl daemon-reload
hopefully this heplful
There's a copy of pg_hba.conf under:
/var/lib/pgsql/data
which is the actual version used by the instance. Put the configuration in this file resolved the issue

tunnel-bastion-client service cannot start

I am trying to establish tunnel bastion connection between two ec2 instance. The client side is allowing Lambda to reach server side RDS. I checked the client side ec2 and find the service tunnel-bastion-client cannot start.
Aug 17 23:07:49 ip-172-31-25-38 systemd[1]: tunnel-bastion-client.service: Scheduled restart job, restart counter is>
Aug 17 23:07:49 ip-172-31-25-38 systemd[1]: Stopped Maintain connection with tunnel bastion.
Aug 17 23:07:49 ip-172-31-25-38 systemd[1]: Started Maintain connection with tunnel bastion.
Aug 17 23:07:50 ip-172-31-25-38 ssh[186032]: Host key verification failed.
Aug 17 23:07:50 ip-172-31-25-38 systemd[1]: tunnel-bastion-client.service: Main process exited, code=exited, status=>
Aug 17 23:07:50 ip-172-31-25-38 systemd[1]: tunnel-bastion-client.service: Failed with result 'exit-code'
For some reason, the tunnel-bastion-client service cannot be started manually. I can connect RDS from server side ec2. So, it is not the database issue.
# psql -h xxxx-xxxx-xxxxxxx.xxxxxxxxxxxxx.us-west-1.rds.amazonaws.com -p 5432 postgres
Password:
psql (10.18 (Ubuntu 10.18-0ubuntu0.18.04.1), server 11.9)
WARNING: psql major version 10, server major version 11.
Some psql features might not work.
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
postgres=>
I used the terraform to standup the both ec2 and grabbing from parameter store. Please help me find out where exactly is the issue. Appreciate in advance.
work around, execute "ExecStart" line from /etc/systemd/system/tunnel-bastion-client.service file.

PG::ConnectionBad Postgres Cluster down

Digitalocean disabled my droplet's internet access. After fixing the error (rollback to older backup) they restored the internet access. But afterwards I constantly get an error when deploying, I can't seem to get my Postgres database up and running.
I'm getting an error each time I try to deploy my application.
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
So I used SSH to login to my server and check if my Postgres was actually running with:
pg_lsclusters
Results into:
Ver Cluster Port Status Owner Data directory Log file
9.5 main 5432 down postgres /var/lib/postgresql/9.5/main /var/log/postgresql/postgresql-9.5-main.log
Postgres server status
So my Postgres server seems to be down. I tried putting it 'up' again with:
pg_ctlcluster 9.5 main start After doing so I got the error: Insecure directory in $ENV{PATH} while running with -T switch at /usr/bin/pg_ctlcluster line 403.
And /usr/bin/pg_ctlcluster on line 403 says:
system 'systemctl', 'is-active', '-q', "postgresql\#$version-$cluster";
But I'm not to sure what the problem could be here and how I could fix this.
Update
I also tried updating the permissions on /bin to 755 as mentioned here. Sadly that did not fix my problem.
Update 2
I changed the /usr/bin to 755. Now when I try pg_ctlcluster 9.5 main start, I get this:
Job for postgresql#9.5-main.service failed because the control process exited with error code. See "systemctl status postgresql#9.5-main.service" and "journalctl -xe" for details.
And inside the systemctl status postgresql#9.5-main.service:
postgresql#9.5-main.service - PostgreSQL Cluster 9.5-main
Loaded: loaded (/lib/systemd/system/postgresql#.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2018-01-28 17:32:38 EST; 45s ago
Process: 22473 ExecStart=postgresql#%i --skip-systemctl-redirect %i start (code=exited, status=1/FAILURE)
Jan 28 17:32:08 *url* systemd[1]: Starting PostgreSQL Cluster 9.5-main...
Jan 28 17:32:38 *url* postgresql#9.5-main[22473]: The PostgreSQL server failed to start.
Jan 28 17:32:38 *url* systemd[1]: postgresql#9.5-main.service: Control process exited, code=exited status=1
Jan 28 17:32:38 *url* systemd[1]: Failed to start PostgreSQL Cluster 9.5-main.
Jan 28 17:32:38 *url* systemd[1]: postgresql#9.5-main.service: Unit entered failed state.
Jan 28 17:32:38 *url* systemd[1]: postgresql#9.5-main.service: Failed with result 'exit-code'.
Thanks!
You better not mix systemctl and pg_ctlcluster. Let systemctl makes the calls to pg_ctlcluster with the right user and permissions. You should start your postgresql instance with
sudo systemctl start postgresql#9.5-main.service
Also, check the errors in the startup log. You can post them too, to help you figure out what's going on.
Your systemctl status also outputs that the service is disable, so, when the server reboots, you will have to start the service manually. To enable it run:
sudo systemctl enable postgresql#9.5-main.service
I hope it helps
It is mainly because /etc/hosts file is somehow changed.I have removed extra space inside /etc/hosts file.Use cat /etc/hosts
Add these lines into the file
127.0.0.1 localhost
127.0.1.1 your-host-name
::1 ip6-localhost ip6-loopback
And I have given permission 644 to /etc/hosts file.It is working for me even after the reboot of the system.

Cant access Postgres, getting an error about “/var/run/postgresql/.s.PGSQL.5432”

I was running a Django application with Postgres as backend database. It was working fine. All of a sudden, today I saw my database connection is refusing in the production server. So I logged into my server and tried to:
psql
and then it was showing this error:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
The file listed above does not seem to exist.
I checked if my Postgres is running or not with:
/etc/init.d/postgresql status
and it was returning with SUCCESS message.
postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2017-11-16 10:03:41 UTC; 55min ago
Process: 4701 ExecReload=/bin/true (code=exited, status=0/SUCCESS)
Process: 4899 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 4899 (code=exited, status=0/SUCCESS)
Nov 16 10:03:41 median systemd[1]: Starting PostgreSQL RDBMS...
Nov 16 10:03:41 median systemd[1]: Started PostgreSQL RDBMS.
Still, I stopped and started postgres again.
I checked if there any other postgres task running or not with:
ps -ef | grep postgres
And it returned:
root 5210 5117 0 10:21 pts/0 00:00:00 grep --color=auto postgres
So there is no other Postgres.
I even checked if my Postgres accidentally got deleted or not with
dpkg -l | grep postgres
and it returned
ii postgresql 9.5+173ubuntu0.1 all object-relational SQL database (supported version)
ii postgresql-9.5 9.5.10-0ubuntu0.16.04 amd64 object-relational SQL database, version 9.5 server
ii postgresql-client-9.5 9.5.10-0ubuntu0.16.04 amd64 front-end programs for PostgreSQL 9.5
ii postgresql-client-common 173ubuntu0.1 all manager for multiple PostgreSQL client versions
ii postgresql-common 173ubuntu0.1 all PostgreSQL database-cluster manager
ii postgresql-contrib-9.5 9.5.10-0ubuntu0.16.04 amd64 additional facilities for PostgreSQL
So there is no rc instead of ii, which means it has not been uninstalled.
I have tried every other solution in the internet but uninstalling Postgres. Didn't work for me. I might be doing something very silly. I don't want to uninstall and lose my data.
Run the following command to check which port your PostgreSQL server is actually running on:
[root#server ~]# netstat -a | grep .s.PGSQL
unix 2 [ ACC ] STREAM LISTENING 2534417 /var/run/postgresql/.s.PGSQL.5432
I can see from this output that my server is listening on port 5432 and also that the file is created in /var/run/postgresql/.s.PGSQL.5432
You can then update /etc/init.d/postgresql to include the lines
PGPORT=5432
export PGPORT
and update your postgresql.conf file which is located in your data directory and set the port key/value to port=5432
and then restart your PostgreSQL server
[root#server] service postgresql restart

'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.