How to run an existing Django application on aws ec2 instance? - python

I am trying to run a Django application on AWS Ec2 instance. I've chosen Ubuntu as my platform. After cloning the git repository, and creating a virtual environment, I have installed all apps in my requirements.txt. When I try to the following lines of code python3 manage.py migrate ; python3 manage.py check ; python3 manage.py runserver the following error is coming up.
django.db.utils.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "columbus_db" connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "columbus_db"
My settings.py file looks like this
DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Database Engine of PostgreSQL Database
'NAME': 'columbus_db', # Database Name
'USER': 'columbus_db', # Database has a Root User
'PASSWORD': 'columbus', # Database Connection Password
'HOST': "localhost", # IP Address for Localhost
}
What can I change in settings.py or Ec2 Instance settings to start the application and see it at Ec2 IP address?

You are missing a running database, the app code except it to be PostgreSQL, you have multiple choices:
Install and run a local PostgreSQL instance directly in your EC2
Use Amazon's managed database RDS
Use Sqlite which is simple to setup and doesn't require more configuration, but your app might required specific PostgreSQL features

Related

Trouble ussing Buddy to test a project using Django REST FRAMEWORK POSTGRE AS DB

Hello I'm currently trying to implement Buddy with my little StartUp that is using DJANGO REST FRAMEWORK as a base. I found a very good example on the site. Unfortunately in the exmaple the used a MySql DB and I'm using Postgre as DB. My settings.py is:
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'DBname',
'USER': 'DBuser',
'PASSWORD': 'DBpassword',
'HOST': '',
'PORT': '5432',
}
}
}
My Buddy execute look somethig like this:
pip install -r requirements.txt
cd Project
python manage.py test
I also created postgre service in Buddy
like version: latest, Hostname: postgres, Port:5432, Login:DBuser, Password:DBpassword, Name of a database to be created on container startup: DBname
When I run the build Test of my project an error message apears like this:
connection = Database.connect(**conn_params)
File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: 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"?
Action failed: see logs above for details
I really don't now how to fix this, it appears that Buddy creates the database correctly but for some reason django does not reconise that it exists.
Pd: The proyect works fine on my laptop and my friends. I'ts only when I tried to run it on buddy where it gives me problems

Django: remotely access a PythonAnywhere MySQL database

I have a Django app (Python 3.4, Django 1.7) on PythonAnywhere, along with a MySQL database.
The database is working fine on the deployed app.
However, I cannot get to connect it to the app on my local machine.
The following error is thrown when I run python manage.py runserver:
django.db.utils.InterfaceError: (2003, "2003: Can't connect to MySQL server on 'mysql.server:3306' (8 nodename nor servname provided, or not known)", None)
These are the attributes I use:
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': '<username>$<database_name>',
'USER': '<username>',
'PASSWORD': '<databse_password>',
'HOST': 'pythonanywhere.com'
}
}
I have also tried mysql.server and mysql.server.pythonanywhere.com as HOST without any more luck.
I think It's not possible to connect directly to your mysqlserver instance from remote, for security reason, the port 3306 is blocked.
They suggest to connect through SSH Tunnel, follow this link.I don't know If you can do an ssh tunnelling within Django, You should probably write a custom configuration. It's simpler to install an SSH Tunnel software on your PC and then connect your Django App to localhost on a port You have to choose.
Bye
As per PythonAnyWhere documentation :
Open a terminal and run below command.
ssh -L 3333:username.mysql.pythonanywhere-services.com:3306 username#ssh.pythonanywhere.com
provide your PAW account login password
replace username with your username.
Open another terminal and run below command.
mysql -h 127.0.0.1 --port 3333 -u username -p
provide your mysql password. Available in settings file.
keep terminal 1 open as long as you are working on terminal 2.
Accessing PythonAnyWhere MySQL from outside
Only Paid account have permission to access remoteserver for mysql database

Setting up Django to connect to local SQLExpress in windows

I have successfully installed and setup Django in windows. I have also created my first project using django default database setting.
However, I would like to change the DATABASE settings to connect to my local SQLExpress.
Here is my DATABASE settings in settings.py file:
DATABASES = {
'default': {
#'ENGINE': 'django.db.backends.sqlite3',
#'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'NAME': 'mytestdb',
'ENGINE': 'sqlserver_ado',
'HOST': 'localhost',
'USER': 'test_user',
'PASSWORD': '1234',
}
}
After I saved the file, I ran into a problem where it says "Could not open a connection to SQL Server [2]".
I am able to connect to my local SQLExpress server using SQL Server Management studio. I know that the server is running. I've tried to google it and tried different setups, and I couldn't get it running.
This is the error that I've got when I ran syncdb command:
python manage.py syncdb
django.db.utils.OperationalError: (com_error(-2147352567, 'Exception occurred.', (0, u'Microsoft SQL Server Native Clien
t 10.0', u'Named Pipes Provider: Could not open a connection to SQL Server [2]. ', None, 0, -2147467259), None), u'Error
opening connection: DATA SOURCE=localhost;Initial Catalog=mytestdb;UID=test_user;PWD=******;PROVIDER=sqlncli10;DataTy
peCompatibility=80;MARS Connection=True')
Here is my installed packages:
C:\windows\system32>pip freeze
Django==1.7.6
django-auth-ldap==1.2.5
django-mssql==1.6.2
pyodbc==3.0.7
python-ldap==2.4.19
pywin32==219
virtualenv==12.0.7
I am using Django development server.
Does the error "Could not open a connection to SQL Server [2]" is more to the user authentication error or couldn't find the SQL server?
In this case, your HOST string needs to contain both computer name or address and an Instance ID Las set during the installation procedure (SQLEXPRESS by default).
Use:
'HOST': 'COMPUTER-NAME\\SQLEXPRESS'
or
'HOST': '127.0.0.1\\SQLEXPRESS'

Setup and connect to database for Django on AWS

I've currently hosted a Django application on EC2 using Apache.
My database engine
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'subscribe',
'USER': '<username>',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '3306',
}
}
After setting up Apache, i can now access my web application on the public IP but I cannot perform any DB transcations as the tables don't exist. This is the error message:
ProgrammingError at /some-url
(1146, "Table 'subscribe.subscriberapp_subscriber' doesn't exist")
I know for sure this is because no migrations have been made post deploying to AWS. My question is, how do I setup the DB completely?
You need to run the initial migrations to create the tables. From the console verify that you have a connection to your db by running ./manage.py dbshell. If that works, you have the connection.
Then you need to either run the initial ./manage.py syncdb (for django <1.7) or if you're running django 1.7+ you will run ./manage.py migrate

Django-postgresql: psycopg2 dsn takes wrong password

I am running python 2.7.3 and django 1.5.1 with postgresql DB. After setting up a simple django app I am getting a OperationalError at FATAL: password authentication failed for user "postgres". When I checked the local variables I found that psycopg2 connection is taking in the wrong password. I had specified the correct password in the django settings.py file.
Also manage.py syncdb works fine, and this problem shows up 2 out 3 times.
All authentication methods in pg_hba.conf is set to md5 and settings.py has HOST: localhost
Don't use the user postgres to do that.
Create a user and a db for your app, and use that user only for that.
su
su postgres
createuser -P <dbusername>
:type and re-type <dbuserpasswd>
createdb -O <dbusername> <dbname>
Check your pg_hba.conf
local all all md5
Restart your postgresql server.
Check your Django Settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'dbname', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'dbusername',
'PASSWORD': 'dbuserpasswd',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
Restart your http server.

Categories