my DB connection is
DATABASES = {
'default': {
'ENGINE': 'djongo',
'ENFORCE_SCHEMA': True,
'NAME': 'taxiapp',
'HOST':'127.0.0.1',
'PORT': xxxx,
}
}
my SSH ADDRESS x.x.x.x
SSH USERNAME 'admin' SSH PORT 0000 SSH AUTH method password and USER PASSWORD 0000
currently, I am getting
pymongo.errors.ServerSelectionTimeoutError: 127.0.0.1:xxxx: [Errno 111] Connection refused
How to define SSH details in my settings.py file ???
You can use Paramiko library for this.
it works pretty well.
here is good explanation of how to set it up.
https://hackersandslackers.com/automate-ssh-scp-python-paramiko/
don't hesitate to forward encountering problems once you do it.
Related
I deployed an application in django on GCP in a VM (ubuntu 22.04 01 LTS)
The app is working normally, database is postgresql.
But I can't remotely access the database, I always get timeout error.
My settings.py
....
DATABASES = {
'default': {
'ENGINE': os.environ.get('DATABASE_ENGINE'),
'NAME': os.environ.get('DATABASE_NAME'),
'USER': os.environ.get('DATABASE_USER'),
'PASSWORD': os.environ.get('DATABASE_PASSWORD'),
'HOST': os.environ.get('DATABASE_HOST'),
'PORT': os.environ.get('DATABASE_PORT'),
}
}
...
My .env file :
# Postgres
DATABASE_ENGINE = 'django.db.backends.postgresql'
DATABASE_NAME = "basededados"
DATABASE_USER = "user"
DATABASE_PASSWORD = "senha"
DATABASE_HOST = "127.0.0.1"
DATABASE_PORT = "5432"
I created the database like this:
sudo -u postgres psql
CREATE ROLE user WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'senha';
CREATE DATABASE basededados WITH OWNER user;
GRANT ALL PRIVILEGES ON DATABASE basededados TO user;
I already set up the postgres.conf file:
listen_addresses = '*'
And the pg_hba.conf file**:**
# IPv4 local connections:
host all all 0.0.0.0/0 md5
I alredy allowed port 5432 through the firewall by executing:
sudo ufw allow 5432/tcp
I'm trying to access the DB like this:
psql -h {Virtual machine IP} -d basededados -U user
the error:
psql: error: connection to server at {Virtual machine IP}, port 5432 failed: Connection timed out
Is the server running on that host and accepting TCP/IP connections?
Am I doing something wrong?
.env
# Postgres
DATABASE_ENGINE=django.db.backends.postgresql
DATABASE_NAME=basededados
DATABASE_USER=user
DATABASE_PASSWORD=senha
DATABASE_HOST=127.0.0.1
DATABASE_PORT=5432
Im trying to use PostgreSQL with django and I get that error when running python3 manage.py migrate:
connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "TheGecko"
I was following this guide:https://djangocentral.com/using-postgresql-with-django/
Here is my settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'websitedb',
'USER':'TheGecko',
'PASSWORD':'xx',
'HOST':'localhost',
'PORT':5432,
}
}
Also, even though I entered that line: GRANT ALL PRIVILEGES ON DATABASE websitedb TO TheGecko;, when I do \l, I get this output. Shouldn't the owner be TheGecko?
I've looked over the web and nothing I could read worked for me.
Please help.
I also revieved same error and resolved by doing below changes:
DATABASES = { 'default': { 'ENGINE':
'django.db.backends.postgresql', 'NAME': 'websitedb',
'USER':'postgres', 'PASSWORD':'xx', 'HOST':'localhost',
'PORT':'5432', } }
I am trying to use my localhost database inside docker Django container.
I have allowed listen_address to all in postgresql.conf file.
I have added host all all localhost,192.168.1.9 trust in pg_hba.conf file.
192.168.1.9 is my en0 address.
Now i want to use 192.168.1.9 as my host in database setting in django.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db_name',
'USER': 'db_user',
'PASSWORD': 'db_password',
'HOST': '192.168.1.9',
'PORT': '5432',
}
}
I am trying this but i am not able to succeed.
Am i doing something wrong? I want postgres to accept all connect so Django app container can connect to my local machine database.
I am trying
psql -h 192.168.1.9 -U db_user -d db_name
Getting psql: could not connect to server: Permission denied
Is the server running on host "192.168.1.9" and accepting
TCP/IP connections on port 5432?
I am not sure what wrong i am doing.
the docker containers are usually on separate network interface (docker0), so if your app wants to access your host you have to use the docker0 interface.
you can get the ip of host from your container with
/sbin/ip route|awk '/default/ { print $3 }'
edited the DATABASES entry in my settings.py to be:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'Limbo',
'USER': '<username>',
'PASSWORD': '<password>',
'HOST': '<dbname>.<gibberish>.us-west-2.rds.amazonaws.com',
'PORT': '5432',
}
}
now when I .manage.py runserver 0.0.0.0:800, it says:
Performing system checks...
System check identified no issues (0 silenced).
then after a minute or two:
File
"/home/ec2-user///local/lib64/python2.7/site-packages/psycopg2/init.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async) django.db.utils.OperationalError: could not connect to
server: Connection timed out
Is the server running on host "..us-west-2.rds.amazonaws.com" (172.rest.of.ip) and
accepting
TCP/IP connections on port 5432?
I have made sure to include the access via 5432 incoming from my ec2's IP (verified with curl ifconfig.co) and the ip listed in the error message (starting with 172 above). perhaps I need to use a larger subnet (than 32)in the 172 source?
EDIT: same error when I run python limbo/manage.py migrate
EDIT2: if I allow 5432 connections from any IP in the security group it works, but as stated above, I am allowing my EC2's IP (according to curl ifconfig.co's return value. what other IP should I be including?
enter the following into the linux command line:
cd /path/to/djangoproj
screen
source <env>/bin/activate
then you should see the command line tool reads something like:
(<env>)[ec2-user#ip-172-31-26-243 djangoproj]$
see that section in the middle: ip-172-31-26-243, that's the local ip, use that in the security group settings. in this case, I used 172.31.26.243/32 as the incoming IP allowed
then try to runserver again
Django db setting is
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
I want to know if I need to use pgpool
Should I need to change this settings ??
update
I try pgpool from this website
And I found that there is no need to modify the code
I don't know Django, but I know pgPool: you can connect to it just like you do with the standard Postgres server, no special config is needed.
Of course Django can connect and can use pgpool. If your pgpool is running on 127.0.0.1 at 5432 then for sure django can work with it. But I will recommend you not to use 5432 as pgppol port because it is the default port of postrgresql. You can use anyother port like 9999 or 7777 etc.
Follow this http://jensd.be/591/linux/setup-a-redundant-postgresql-database-with-repmgr-and-pgpool
to configure your postgresql in replication mode and failover.