Cannot connect to postgresql through sqlalchemy from different computer - python

I think do not understand properly how does sqlalchemy works, I have tried to connect to postgresql running on some cloud server from my local computer:
db = create_engine('postgresql://ubuntu#172.23.160.212:5432/dbname')
but that causes the error:
Is the server running on host "172.23.160.212" and accepting
TCP/IP connections on port 5432?
I have checked the port and host also exists.
I thought I should connect to the host using ssh first:
with SSHTunnelForwarder((172.23.160.212, 22), ssh_username='ubuntu', remote_bind_address=(127.0.0.1, 3306)) as server:
db = create_engine('postgresql://postgres#127.0.0.1:5432/dbname')
But that did not help.

I have solved the problem partially,
If one opens ssh connection in bash (ssh ubuntu#172.23.160.212 -L 5432:localhost:5432 -N -n -f), then one can open db through python:
db = create_engine('postgresql://tissuemaps#localhost:5432/dbname')
If I understand correctly, the connection to postgres directly should also have worked, and why it does not, I do not know.

I think the problem is TCP connection is not enabled and have to modify your pg_hba.conf file to allow the connection. Add lines in the config file to allow connection
host all all 0.0.0.0/0 md5
host all all ::/0 md5
Apart from that you can also check postgresql.conf (/etc/postgresql/9.3/main/postgresql.conf) to check other postgres configs are what you expect like port number etc. Also add below line in config file to accept all the connections
listen_addresses = '*'
You need to restart the postgres service for the changes to be picked up
sudo service postgresql restart
https://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html
https://www.postgresql.org/docs/9.1/static/runtime-config-connection.html

Related

Access on local PostgreSQL Database from the inside of a docker container with a flask-webapp using a psycopg2 connection

I am relatively new to this topic. I try to build a webapp using flask. The webapp uses data from a postgresql database which is running local (Mac OS Monterey 12.2.1).
My application uses a python code which accesses data from the database by connecting to the database with psycopg2:
con = psycopg2.connect(
host = "192.168.178.43"
database = self.database,
port = "5432",
user = "user",
password = "password")
I already added the relevant entries to the "pg_hba.conf" file and to the "postgresql.conf" file to the needed configurations for an access in my home network. But i still got an error when starting the container. The app runs perfect outside the container. I think I miss some important steps to complete a successful connection.
This error is the following
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?

docker container connect local postgresql

I run python container, I want to connect localhost postegresql. And I try some method. But not work. Please talk me. How can I do ? Thanks.
I have run postegresql on port 5432, create datatbase and grant user.
run docker comand
docker run --name=python3 -v ${pwd}:/code -w /code python
python code
import psycopg2
def main():
#Define our connection string
conn_string = "host='localhost' dbname='testDB' user='test' password='test'"
# print the connection string we will use to connect
print ("Connecting to database\n ->{}".format(conn_string))
# get a connection, if a connect cannot be made an exception will be raised here
conn = psycopg2.connect(conn_string)
# conn.cursor will return a cursor object, you can use this cursor to perform queries
cursor = conn.cursor()
print ("Connected!\n")
if __name__ == "__main__":
main()
error message
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
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?
It depends on your host OS and your docker version.
I assume here your database is not running in a container itself, but rather on the host itself (localhost).
For instance, as mentioned in "From inside of a Docker container, how do I connect to the localhost of the machine?", with Docker for Mac v 17.06 and above (June 2017), you can connect to the special Mac-only DNS name docker.for.mac.localhost which will resolve to the internal IP address used by the host.
On Linux directly, you would use the host mode, with an image having ifconfig installed:
docker run --network="host" -id <Docker image ID>
Reading that you're on Windows 10 and running postgresql on the host, I advise you to run postgresql in a container. It makes this way easier.
To connect the python container to the postgres container you'll need a docker network though. Let's call it postgres_backend.
docker network create postgres_backend
You can create the postgresql container with the following command. Just change the /path/to/store/data to a local directory in which you'd like to store the postgres data:
docker run --name postgres \
-e POSTGRES_PASSWORD=test \
-e POSTGRES_USER=test \
-d --restart always \
-v /path/to/store/data:/var/lib/postgresql/data \
--net postgres_backend \
postgres:9.6
Now your postresql container should be up and running :)
To connect your python container to it, you'll have to add a --net postgres_backend to your docker run command and change the host in your script to "postgres" (it's the name we gave the postgres container with --name).
If the python container can't find the host "postgres", try it with the IP shown when entering the command docker exec -ti postgres ip addr show.
to fix this bug:
First, it is normal that it does not work, because postgresql does not run in the same container as the application so the host localhost: 5432 does not exist.
to fix it :
on the properties file isntead localhost:5432 use your IPadresse like IP:5432 and in the pg_hba.conf add this
host all all 0.0.0.0/0 md5

How to connect to myql in docker container from CLI or python script

I want to connect to MySQL inside Docker container. I have a running instance of MySQL in a docker container. Since I already have 3306 port busy on my host, I decide to use port 8081 to start my MySQL container. Basically, I started my container with docker run -p 8080:80 -p 8081:3306 --name test test. When I connect to my container, I can connect to my MySQL without error. On the other hand, I have a web app that is able to connect to MySQL with the exact same port (8081) from my host. This means that MySQL is working properly and is reachable from outside. But now in my python script, I cannot connect. I am unable to connect to MySQL with CLI either. It seems like the port number is simply not interpreted. I see that if use for example mysql -P 8081 -u root -p. This is just trying to connect to host MySQL (port 3306) instead of container MySQL on port 8081(when I enter host MySQL credentials, it connect to host MySQL). In my python script, I used this: conn = MySQLdb.connect( host = 'localhost', port=8081, user='root', passwd=''). But this is not working either. In the MySQL man page, I see this :
ยท --port=port_num, -P port_num
The TCP/IP port number to use for the connection.
What am I doing wrong, please?
mysql --version:
mysql Ver 14.14 Distrib 5.7.18, for Linux (x86_64) using EditLine wrapper
update
here is my docker ps:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b370c91594d3 test "/bin/sh -c /start.sh" 14 hours ago Up 14 hours 8080/tcp, 0.0.0.0:8080->80/tcp, 0.0.0.0:8081->3306/tcp test

Operational Error(2003, "Can't connect to MySQL server on '127.3.138.130' (111)")

I am trying to connect MySQL by using ip which I got from PhpMyAdmin using python. But I face Operational Error(2003, "Can't connect to MySQL server on '127.3.138.130' (111)")
I know how to use mysql to connect to localhost
I am following this tutorial
I have written following lines of code in python
I am using ubuntu terminal to run python code and I do have mysql-server installed on my laptop.
import MySQLdb
db = MySQLdb.connect('127.3.138.130','my_username','my_password','my_db_name')
So what the problem is ? how to solve this problem ,please explain me in very simple manner. Thanks!
Make sure the server listens to outside requests. To do this go to /etc/mysql/my.cnf on the server and edit:
bind-address = 127.0.0.1
To:
bind-address = 0.0.0.0
You may need to use sudo when editing the file.
You will need to restart the server after changing the configuration:
sudo service mysql restart
You can also see what port is the server listening on (the default being 3306) in /etc/mysql/my.cnf. Just look for a line that says:
port = 3306
Make sure you're connecting to through same port, this is how you can specify a port:
db = MySQLdb.connect(
host = '127.3.138.130',
user = 'my_username',
passwd = 'my_password',
db = 'my_db_name',
port = 3306 # should be same as in /etc/mysql/my.cnf on server.
)
use 'pymysql' lib...it may help u....
import pymysql
conn = pymysql.connect(host=''127.3.138.130', port=3306, user='root', passwd='password', db='dbname')

Postgresql socket error when running Django's syncdb (mac OS 10.7.5)

I'm trying to run Django's syncdb operation and am getting the following error:
psycopg2.OperationalError: could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I've read the answers to this question and have tried the following:
changing "listen_address" to "localhost" or "*" in /Library/PostgreSQL/9.1/data/potsgresql.conf
changing "host" to "localhost" in my settings.py file (for Django)
user is set to "postgres" with correct password in settings.py
(I restarted the postgresql server after each step.)
I've also checked my pg_hba.conf file to see if the Unix domain socket was accepting connections from all users (based on this page). Here's what I have there:
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
I never had this problem before. I somewhat recently upgraded to Lion and am working on a new virtualenv with the bare minimum of modules installed.
Found the answer courtesy of Bradley Ayers:
set "unix_socket_directory" to "/var/pgsql_socket/" in postgres.conf
executed following commands:
"$ sudo dscl . append /Groups/_postgres GroupMembership postgres"
"$ sudo chmod g+w,o+rx /var/pgsql_socket/"
restarted postgresql
your database is not running try
pg_ctl -D /usr/local/var/postgres start
assuming thats where your postgre data directory is

Categories

Resources