Connect to remote postgresql server on amazon ec2 - postgresql

I started an amazon ec2 instance, and installed postgresql 9.1 over it. I then went to the
Security Group: quicklaunch-1(there was one moredefault` which i did not change) and opened the 5432 TCP Port, the table looks like this:
(Service) Source Action
22 0.0.0.0/0 Delete
5432 0.0.0.0/32 Delete
5433 0.0.0.0/32 Delete
6432 0.0.0.0/32 Delete
I have created a database and user .
My /etc/postgresql/9.1/main/pg_hba.conf looks like this:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
host all all 0.0.0.0/0 md5
host db_name user_name 0.0.0.0/0 md5
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
host replication postgres 127.0.0.1/32 md5
host replication postgres ::1/128 md5
and /etc/postgresql/9.1/main/postgresql.conf looks like this:
# - Connection Settings -
listen_addresses = '*'
#listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost', '*' = all
# (change requires restart)
port = 5432 # (change requires restart)
I then try to connect on to the remote machine as follows:
psql -h ec2-xxx-xx-xxx-xxx.compute-1.amazonaws.com -d <database_name> -U <username>
where ec2-xxx-xx-xxx-xxx.compute-1.amazonaws.com is my Public DNS.
The above command does not result in any connection, how can i connect?

In this table:
5432 0.0.0.0/32 Delete
5433 0.0.0.0/32 Delete
6432 0.0.0.0/32 Delete
the CIDRs look like you're not allowing any IP in. Shouldn't they be 0.0.0.0/0 instead, like what you have for port 22 (ssh)?

I Found the resolution to this problem.
Two things are required.
Use a text editor to modify pg_hba.conf.
Locate the line host all all 127.0.0.1/0 md5.
Immediately below it, add this new line: host all all 0.0.0.0/0 md5
Editing the PostgreSQL postgresql.conf file:
Use a text editor to modify postgresql.conf.
Locate the line that starts with #listen_addresses = 'localhost'.
Uncomment the line by deleting the #, and change localhost to *.
The line should now look like this: listen_addresses = '*' # what IP address(es) to listen on;.
Now Just restart your postgres service and it will connect

your psql command needs a -W option added, which allows you to enter a password for the db user and a -p option followed by the postgres port number (5432 per default)
cheers!

Related

After changing the port number, connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"

I finished install postgresql11 and to change port number
The first file I modified was '/var/lib/pgsql/11/data/postgresql.conf'
#listen_addresses = 'localhost' -> listen_addresses ='*'
#port = 5432 -> port = 9485
The second file I modified was '/var/lib/pgsql/11/data/pg_hba.conf'
the line i added is 'host all all 0.0.0.0/0 md5'
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
i restarted postgresql server
systemctl restart postgresql-11
Connection from external client to dbeaver works fine.
but local connection is not fine
su - postgres
psql
there is error message
psql: could not connect to server: There is no such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I can find two files in '/var/run/postgresql'
.s.PGSQL.9485
.s.PGSQL.9485.lock
If Ireturn the port to the original number of 5432, it works normally again.
Please tell me how to fix it
psql is not aware of your Postgres settings, it is using the default port 5432.
Try: psql -p 9485
To complete your setup you have to change environment variable PGPORT
PGPORT=9495; export PGPORT;
If not you have to give port number ( -p 9495 ) to every cde (psql, pg_dump, ...)

Unable to connect to an postgresql ec2 instance from pgadmin4

I created an EC2 instance and then installed postgresql (version 11) and timescaledb in it. My security group looks as follows:
As of now, I have enabled this instance for all the IP addresses.
My /etc/postgresql/11/main/pg_hba.conf looks like this:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
host all all 0.0.0.0/0 md5 #ipv4 range
host all all ::0/0 md5 #ipv6 range
host all all all md5 #all ip
and the /etc/postgresql/11/main/postgresql.conf looks like this:
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
Then, I try to connect to the database via Pgadmin4 using the Public DNS(IPv4):ec2-xx-xxx-xxx-xxx.eu-central-1.compute.amazonaws.com
After I enter the username and password, it throws the following error:
Unable to connect to server:
could not connect to server: Connection refused
Is the server running on host "ec2-xx-xxx-xxx-xxx.eu-central-1.compute.amazonaws.com" (xx.xxx.xxx.xxx) and accepting
TCP/IP connections on port 5432?
I had a look at the following post and followed all the instructions, but to no avail:
connect-to-remote-postgresql-server-on-amazon-ec2
Can anyone please help me out with this?
Edit 1
When I do:
/etc/init.d/postgresql restart
I get:
[....] Restarting postgresql (via systemctl): postgresql.service==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'postgresql.service'.
Authenticating as: Ubuntu (ubuntu)
Password:
I don't know what password to enter here so that the postgresql can get restarted
As pointed out by #jjanes in the comments, I had to restart the postgresql inorder to access it.
Doing
/etc/init.d/postgresql restart
was throwing an error (as shown in the question) that's why I had to use
sudo /etc/init.d/postgresql restart
in order to grant the root access.
Once the db was restarted, I tried connecting to the ec2 instance again via pgadmin4, and this time it worked.

Postgres remote connection - Windows server

I have the following lines in pg_hba.conf. Postgres is installed on a Windows server.
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 md5
#host all all myip md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
I would like to allow connection only from one more public IP address. How can I achieve that? As soon as I enable the above line IP: Postgres doesn't start.
Looking for some guidance.
To open the port 5432 edit your /Program Files/PostgreSQL/10/data/postgresql.conf and change
# Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
In /Program Files/PostgreSQL/10/data/pg_hba.conf
# IPv4 local connections:
host all all 0.0.0.0/0 md5
Now restate the Postgres server use cmd
pg_ctl -D "C:\Program Files\PostgreSQL\10\data" restart
I run postgres on several Windows servers (Windows Server 2012 R2, Windows Server 2016) as a part of a commercial framework running on an Apache tomcat webserver. The local connection worked fine. However, I want the same postgres server to be accessible for another framework (Cakephp) on another server (Red Hat Enterprise Linux) on the same server farm. This used to work until I upgraded to postgres 9. Now I had to upgrade to postgres 10. No matter what I tried, I failed.
Here is what I did to solve the problem:
Find your local postgres configuration files. They are usually in the same directory as the postgres tablespace, in my case:
d:\PG10Data\postgresql.conf.
This file has to contain the following lines:
# - Connection Settings:
listen_addresses = '*' # what IP addresses/interfaces to listen on
port = 5432
The next file to be modified is the pg_hba.conf (hba = host based access):
# TYPE DATABASE USER ADDRESS METHOD
# some sample entries:
# this will open UP ALL REMOTE IPv4 connections, do not open up permanently
host all all 0.0.0.0/0 md5
# only 1 database for 1 user from 1 IPv4 address:
host yourdatabasename yourusername 10.15.17.13/32 md5
# don not forget the "/32", otherwise the postgres server will not start up!
After editing these files, restart the postgres server.
You can run
netstat -a -n | findstr 5432
to see if the postgres listener is running.
You can also run the following to test connectivity from the Windows command prompt:
psql -Uyourusername -dyourdatabasename -p5432 -hlocalhost
This should work at any time. The next level will be to use your computer's local IPv4 address. This you can find out with
ipconfig
This will tell you your local IPv4 address. Use this in the following command:
psql -Uyourusername -dyourdatabasename -p5432 -hyourlocalip
My problem was, this command failed. Since I ran it directly on my server, it could not be the local Windows firewall.
The solution:
There is a 2nd configuration file:
d:\PG10Data\postgresql.auto.conf
The file starts ominously with the following 2 lines:
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command
And it ends:
listen_addresses = 'localhost'
I tried the ALTER SYSTEM command when I was logged on locally as postgres via psql, but no success.
In the end I turned cheeky and changed the entry in d:\PG10Data\postgresql.auto.conf to;
listen_addresses = '*'
Bingo! After a postgres restart, remote access worked like a charm on both Windows servers.
N. B. Don't forget the Windows Firewall: Open port 5432 for remote access. Also check there are no firewalls in the network the block access to port 5432 from your remote client.
It would be nice if someone were able to tell me how I should have changed the parameter without editing the postgresql.auto.conf, but at least both of my framesworks - both on the local and the remote server - are working.
That is incorrect syntax: myip is not an IP address, and the /32 after it is missing.

docker app postgres local connection

I have created a docker image with playframework backend that connects to pgsql localhost database. It is running smoothly when I run it in my local machine but when I run it in docker I receive errors.
I want to connect the docker container to my posgresql localhost.
I have tried configuring postgresql.conf and pg_hba.conf to open the port 5432
pg_hba.conf
# TYPE DATABASE USER ADDRESS
METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 192.168.1.0/24 trust
host all all 172.17.0.0/16 trust
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 trust
host all all ::/0 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all 172.17.0.0/32 trust
host replication all ::1/128 trust
postgresql.conf
listen_addresses = '*'
# what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432
When running my playframework app. I am getting the error below
Caused by: com.zaxxer.hikari.pool.PoolInitializationException: Exception during pool initialization
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
On a side note, postgresql is installed using brew, running correctly can access database using pgadmin4.

postgres hba.conf for jdbc

I'm trying to set up a JDBC connection to postgres from another program.
When I run the program, I get the following error:
[2013-03-24 03:14:10,542][ERROR][org.elasticsearch.river.jdbc.strategy.simple.SimpleRiverSource] while opening read connection: jdbc:postgresql://[my postgres server's ip address]:5432/[my database name] FATAL: no pg_hba.conf entry for host "[my client ip address]", user "postgres", database "[my database name]", SSL off
I can connect to the server successfully with a command line client by running:
psql -d [my database name] -U postgres -p 5432 -h [my postgres server ip address]
I think then that I have a unix socket connection that works but need to enable tcp connection in the pg_hba.conf, but it looks like I already have several tcp connections allowed in the pg_hba.conf file below... can anyone advise what I may be doing wrong?
My pg_hba.conf contains the following uncommented lines:
hostssl all all 0.0.0.0/0 md5
host all all 10.0.0.0/8 md5
local all postgres peer
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
If doing devel on local computer and booth program and DB run on the same machine go with:
# IPv4 local connections:
host all all 127.0.0.1/32 trust