Connect local postgres database from PGAdmin Container - linux

I am trying to connect to my local postgres database from PGadmin in container, but it is through following error:
Unable to connect to Server: connection to server at "localhost" (127.0.0.1), port 5432 failed. connection refused is the server running on that host and accepting TCP/IP connections?
configurations i am trying for creating server are as follows:
hostname/address = localhost
port = 5432
username = postgres
my OS configurations
OS = Linux- Ubuntu
Version = 18.04
PS: there are some existing questions regarding this on Stackoverflow, but no solution helped me.

While it runs locally, the docker container is like a different machine. You need to put the IP address of the local host machine instead of localhost.

Related

Not able to connect to EC2 instance in AWS via python/psycopg2

I am having trouble connecting to aws via python. I have a macOSX operating system.
What i did:
I created an ec2 instance and chose an operating system (ubuntu) and downloaded postgresssql in the remote server. Then i created securitygroups where i added the following configuration:
type:
ssh
protocol:
tcp
port: 22
source: custom, 0.0.0.0/0
Then i added another rule:
postgresql
TCP
5432
custom
my_computer_ip_address 71.???.??.???/32
where i added question marks just to hide the address. but its in that format.
Now, aws had me create a .pem file in order to query from the database. I downloaded this pem file into a secret location.
When i go to my local machine, go to my terminal and type:
ssh -i "timescale.pem" ubuntu#ec2-??-???-??-???.compute-1.amazonaws.com"
i am able to connect. I also went to my dbeaver and created a new connection and set up a connection where i am using an ssh tunnel and a public key to read the 'timescale.pem' file i created. I then go to main and type my username and password:
username: postgres
database: example
password: mycustompassword
and i am able to connect with no issues.
Now, when I go to python with psycopg2 library, i am just unable to connect at all. I have gone through all the examples here in stackoverflow and none of them have helped me. Heres what i am using to connect to aws from python:
path_to_secret_key = os.path.expanduser("~/timescale.pem")
conn = psycopg2.connect(
dbname='example',
user='postgres',
password='pass123',
host='ec2-??-??-??-???.compute-1.amazonaws.com',
port='22',
sslmode='verify-full',
sslrootcert=path_to_secret_key)
I then get this error:
connection to server at "ec2-34-???-??-???.compute-1.amazonaws.com" (34.201.??.???), port 22 failed: could not read root certificate file "/Users/me/timescale.pem": no certificate or crl found
Ok...then i switched ports and added '5432' and get this warning:
connection to server at "ec2-??-???-??-???.compute-1.amazonaws.com" (34.???.??.212), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
When i ssh into my terminal and type: netstat -nl |grep 5432 i get the following:
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN
unix 2 [ ACC ] STREAM LISTENING 812450 /var/run/postgresql/.s.PGSQL.5432
Can someone please help? Thanks
The .pem file is for connecting to the EC2 instance over SSH, on port 22. PostgreSQL is running on port 5432. You don't use the .pem file for database connections, only for ssh connections. You need to change your Python script to use port='5432', to connect directly to the PostgreSQL service running on the EC2 instance.
This seems to work now:
from sshtunnel import SSHTunnelForwarder
mypkey = paramiko.Ed25519Key.from_private_key_file(path_to_secret_key)
tunnel = SSHTunnelForwarder(
('remote_public_port', 22),
ssh_username='ubuntu',
ssh_pkey=mypkey,
remote_bind_address=('localhost', 5432))
tunnel.start()
conn = psycopg2.connect(
dbname='example_db',
user='postgres',
password='secret123',
host='127.0.0.1',
port=tunnel.local_bind_port)

Docker Postgresql can't connect remotely

I can connect my docker postgresql in my local server windows via ip or localhost. When I try to connect it remotely from my personal windows I have timeout error. Thinking about 5432 port is blocked or not listening.
I have changed pg_hba.conf & postgresql.conf files for remote connection and also checked for firewall but it was disabled.
Answer is I should have checked my server's firewall conf also, just dockerVM is not enough :)

Cant connect to postgresql server running in windows 10 virtual machine

I'm running a postgres server in a windows 10 virtual machine on my pop-os laptop. I'm trying to connect to it from my host OS (linux). In virtual box I created a port forwarding rule :
I also changed the listening port in the postgres configuration to equal '*'.
The error message I get trying to connect is this:
postgres#pop-os:/home/peyton$ psql -p 5432
psql: error: could not connect to server: 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"?
I don't understand this very well so I could really use some help.
Thanks.
The problem is:
psql -p 5432
is saying connect to local socket on Linux machine. Per the error message:
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
You need to do something like:
psql -p 5432 -h 127.0.0.1

Odoo container : Database connection failure: could not connect to server: Connection refused (linking odoo with localhost postgres)

I'm trying to run the odoo docker container and linking it with my local (not container) postgresql.
I tried this command (as suggest here).
docker container run -p 8089:8069 -e HOST=127.0.0.1 -e USER=tux -e PASSWORD=tux --name odoo -t odoo
Ran it and got these errors :
Database connection failure: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
I tried to open up the port 5432 with ufw but got the same erros. I also tried to change 127.0.0.1 to 0.0.0.0 but nothing.
Could you help me?
ps : the USER and PASSWORD are corrects, I use them when I run odoo instance in my local.
you might need to add the host network with a flag --network=host
WARNING: Published ports are discarded when using host network mode
that just means you cannot change the port bindings. thats as secure as your machine is.

As I can, configure the firewall of ubuntu server for the server to accept connections of the terminals through PostgreSQL port 5432

Configuration: Server: Ubuntu server 16.04 LTS using webmin
Terminal: Windows 7 Using PgAmin III
I was unable to establish the connection between my terminal and my server through pgAdmin III on port 5432.
On my server I added:
in file postgresql.conf I edited
in #Connection Settings
listen_addresses = '*'
in file pg_hba.conf I added
in #IPv4 local connections
host all all 172.x.x.x/32 md5 //this is IP Terminal (Hidden x)
I checked the port, this is 5432 default and user is postgres
When I try to establish the connection on PgAdmin III:
Host: //My Server IP (Ping console successful)
Port: 5432
username: postgres
password: //My password
Show me the following message:
Server doesn't listen
The server doesn't accept connections: the connection library reports
could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "Mi SERVER IP Hidden" and accepting TCP/IP connections on port 5432?
If you encounter this message, please check if the server you're trying to contact is actually running PostgreSQL on the given port. Test if you have network connectivity from your client to the server host using ping or equivalent tools. Is your network / VPN / SSH tunnel / firewall configured correctly?
For security reasons, PostgreSQL does not listen on all available IP addresses on the server machine initially. In order to access the server over the network, you need to enable listening on the address first.
For PostgreSQL servers starting with version 8.0, this is controlled using the "listen_addresses" parameter in the postgresql.conf file. Here, you can enter a list of IP addresses the server should listen on, or simply use '*' to listen on all available IP addresses. For earlier servers (Version 7.3 or 7.4), you'll need to set the "tcpip_socket" parameter to 'true'.
You can use the postgresql.conf editor that is built into pgAdmin III to edit the postgresql.conf configuration file. After changing this file, you need to restart the server process to make the setting effective.
If you double-checked your configuration but still get this error message, it's still unlikely that you encounter a fatal PostgreSQL misbehaviour. You probably have some low level network connectivity problems (e.g. firewall configuration). Please check this thoroughly before reporting a bug to the PostgreSQL community.

Resources