URL to ping Postgres DB to check uptime - postgresql

We have set up a series of uptime checks on all of our online services.
The last one is our Postgres DB. Given the connection URL in the format of:
postgres://username:password#domain-name:port/restriction
Does postgres comes out of the box with an http-callable url that would return a 200 if DB is up and 500 or nothing is DB is down?

You can use pg_isready to check the connection status of a PostgreSQL server
pg_isready --dbname=dbname --host=hostname --port=port --username=username
From the documentation:
pg_isready returns 0 to the shell if the server is accepting
connections normally, 1 if the server is rejecting connections (for
example during startup), 2 if there was no response to the connection
attempt, and 3 if no attempt was made (for example due to invalid
parameters).

No, it does not speak HTTP. You'd have to talk to the database using its driver software or make a small web service that does nothing but ping the database on demand.

Related

Why does `pg_isready` not respect database option?

When I run pg_isready with database option, I always get response that everything is ok:
pg_isready -h pgdb -U pdnsdf -d askldfjkasdjf
pgdb:5432 - accepting connections
Here database does not exists. So I expect it is not ready.
Why does pg_isready not respect database option?
Quote from the manual
It is not necessary to supply correct user name, password, or database name values to obtain the server status; however, if incorrect values are provided, the server will log a failed connection attempt.
If you want to see the error, you have to check the log file of the server.
Here database does not exists. So I expect it is not ready.
pg_isready checks if the server (instance, cluster, ..) is ready to accept connections. It is not intended to check if a specific database (or user or table) inside that cluster exists.

PostgresSQL is not getting connected

I am new to Ubuntu operating system and trying to install postgresSQL in my system.
After completing the installation and entering postgres mode when I enter the command pgsql, it gives me this error
psql: error: 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"?
what could possibly be wrong??
Probably the postgresql server is not running; you can run netstat -adp to see if postgres is listening on port 5432.
If it isn't, then it most likely isn't running; systemctl status postgresql will help identify if it is even installed, and if it has run into any issues (for further details, see https://mydbanotebook.org/post/troubleshooting-01/)
If it is running and seems healthy, then it might not be configured to accept connections in whichever way you are connecting (which user, database, host/network etc) - see details about the pg_hba.conf file that includes rules for this - https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html
After install, you must create an instance with initdb.
Later, change the listen_address to *, restart the service and now you can check the port.
Check the pg log for the message: "postgres is ready to accept connections"

How to open a postgres database created using psql in PgAdmin4?

How do I open a postgres database created using psql (i.e. in the terminal) in PgAdmin4 and vice versa?
I also noticed that the localhost port for psql is 5432 and for PgAdmin4 is 5433.
Is this correct?
Some background is in order. When you install Postgres you create an instance of a server that comes with three databases already created; template0, template1 and postgres. On a given machine you can create more then one Postgres server/instance and have them run simultaneously. In order for that to happen though each server needs to listen on a different port. The default port is the 5432 you mention above. It would seem you also have another server running on port 5433. I'm guessing what you want to know is about connecting to a given server rather then a database in that server. In that case it is important to know that Postgres works on the server/client model where it is the server and in your case psql and pgAdmin are the clients. What this means is that a Postgres server is not tied to a client, it exists on its own. It also means a client can connect to any Postgres server it can reach, assuming it is using the correct credentials. All of the previous means, yes you can connect psql and/or pgAdmin to either server. For psql specify the correct port using -p. For pgAdmin you will need to set up a server using the server dialog Dialog. Then use the appropriate port in the connection tab.

AWS instance connection refused restored db instance

I cannot connect to a restored rds instance.
I create a new rds instance by restoring_from_snapshot.
It copies all settings except the security group so I modify it after boot.
Now I cannot get psql to connect to 5432 on the endpoint.
Despite identical settings
old working instance (can be access is in live usage)
new test instance based on snapshot
I have the exact same settings on both instances. The new instance starts with the default vpc sec group but I modified it to have the same.
The Instance also had many hours trying to propagate dns info. I have been trying the whole day to fix this.
What could I have possibly missed here.
The difference between the old(live) and new(test) is that the latter has no Multi AZ, no Backups, no Minor Version Upgrade. Ports are all default postgres 5432
psql response is that it asks for password with the new it says
psql: could not connect to server: Connection refused
Is the server running on host "**********.eu-central-1.rds.amazonaws.com" (**.**.**.**) and accepting
TCP/IP connections on port 5432?
It seems our company firewall won't let me out on port 5432.

Connecting two servers with PgPool

I'm trying to configure pgpool to use load balance between two servers (Both running Debian 8.2 and Postgresql 9.4).
I already have Streaming Replication working between the two (Master in 153 and Slave in 155). Now I installed PgPool and configured with the two servers:
backend_hostname0 = '10.0.0.153'
backend_port0 = 5432
backend_weight0 = 1
backend_data_directory0 = '/var/lib/postgresq/9.4/main'
backend_hostname1 = '10.0.0.155'
backend_port1 = 5432
backend_weight1 = 1
backend_data_directory1 = '/var/lib/postgresq/9.4/main'
and configure pool_hba, pool_passwd, and load_balance_mode is on.
My problem is: When i try to use psql via pgpool it displays an error:
"psql: FATAL: password authentication failed for user 'postgres'"
BUT if I comment all the section of backend1, changing nothing more, and restart the pgpool2 service, I can connect without problem, in the same machine, using the exact same user and password. I don't know if there's another parameter that I should set in order to use two servers and load balance between them.
I can connect to each backend server from each client with psql just fine, so credentials shouldn't be the problem.
PS: Don't know if that helps. But in pgpool.conf, the replication_mode is off, because I'm using the Streaming Replication, and as far as I heard, it's possible to use load_balance without making replication via pgpool.
Thanks.
Try to login to pgppol server and run the command:
pg_md5 -m -u postgres your_password_for_postgres_user
Now see the content of pool_passwd. It should contain something like:
postgres:md55ebe2294ecd0e0f08eab7690d2a6ee69
Now restart your pgpool and try to connect again. Hopefully your problem will be resolved.
pgpool's load balancing can be used with streaming replication, for that you need to set master_slave_mode and load_balance_mode to true and also set master_slave_sub_mode to stream.
For the authentication error you are getting, check the pg_hba.conf settings for pgpool host. And depending on the type of authentication method specified see the respective client authentication section in the pgpool documentation.
http://www.pgpool.net/docs/latest/pgpool-en.html#hba