How To Use PostgreSQL with your Django Application on Ubuntu 14.04
How To Use PostgreSQL with your Django Application on Ubuntu 14.04
If you're developing a Django application and want to use PostgreSQL as your database, this tutorial will guide you through the process on Ubuntu 14.04. PostgreSQL is a powerful open-source relational database management system that can handle large amounts of data and transactions.
Step 1: Install PostgreSQL
To install PostgreSQL on Ubuntu 14.04, run the following command:
sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
This will install PostgreSQL and the additional contrib package, which provides some additional features and utilities.
Step 2: Create a PostgreSQL user and database
Once PostgreSQL is installed, you need to create a user and database for your Django application. To do this, follow these steps:
- Switch to the postgres user:
- Create a new user:
- Create a new database:
- Exit the postgres user:
sudo su - postgres
createuser --interactive
Follow the prompts to create a new user with the desired name and privileges. For a Django application, you should grant the user the ability to create databases:
Enter name of role to add: django
Shall the new role be a superuser? (y/n) y
Create database with the same name as the user? (y/n) y
createdb mydatabase
Replace "mydatabase" with the name of your desired database.
exit
Step 3: Configure your Django application to use PostgreSQL
To configure your Django application to use PostgreSQL, you need to modify your project's settings.py file. Locate the DATABASES dictionary and replace the default SQLite configuration with the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'django',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '',
}
}
Replace "mydatabase" with the name of your database, "django" with the name of your PostgreSQL user, and "mypassword" with the password for your PostgreSQL user.
Step 4: Run database migrations
With your Django application configured to use PostgreSQL, you can now run database migrations to create the necessary tables and fields. To do this, run the following command:
python manage
Комментарии
Отправить комментарий