Setup and connect to database for Django on AWS - python

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

Related

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

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

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

Unable to makemigrations with PostgreSQL setup

I am trying to setup PostgreSQL locally on my computer, but when I try to initially set up my Django application with it using python manage.py makemigrations, I am given this warning:
RuntimeWarning: Got an error checking a consistent migration
history performed for database connection 'default': fe_sendauth: no password supplied
My database table in my settings.py is as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get("DB_NAME"),
'USER': os.environ.get("DB_USER"),
'PASSWORD': os.environ.get("DB_PASSWORD"),
'HOST': os.environ.get("DB_HOST"),
'PORT': os.environ.get("DB_PORT"),
}
}
My .env file is located in the same directory as my manage.py file:
DB_NAME=Smash
DB_PASSWORD=root
DB_USER=SmashDatabase
DB_HOST=localhost
DB_PORT=5432
DEBUG=1
I tried following this link's instructions but none of the offered solutions fixed the problem. I don't know what is wrong or what causes this issue.

Cannot connect to postgresql databse from django settings.py on localhost

Forgive me as I this is my first django project using postgresql (version 11.8). For now I just want to connect to a test database which I have set up locally using pgadmin4. When I create the database I am not given the option to add a password, but when I run python manage.py migrate it is insisting on a password. When I then set the password to "password" on pgadmin, django won't accept it. It's probably something really obvious as I am quite new to Django, but I have tried searching and not found the answer to my problem. In settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'test1',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Last line of the error when I run python manage.py migrate:
django.db.utils.OperationalError: fe_sendauth: no password supplied
Any help much appreciated. Craig
When you installed PgAdmin4 it asked you to create a password. This is for the superuser postgres, which is what you are trying to connect as. Use psql to connect to the database using the above settings and supply the password and see if it works.
I resolved it by removing the Postgresql 11 server and reinstalling it.

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'

Categories