I'm hosting my Django project with Apache on a Ubuntu 20.04 server with the standard SQLite database. It works but the database seems to be read-only.
When we write to the database we get an OperationalError:
attempt to write a readonly database
I have tried to change the permissions according to the guide below
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-16-04
I've changed the permissions of the sqlite file to 664 and gave user www-data ownership of the file and the parent folder.
Yet I'm still getting the same error. Any ideas what else could be causing this problem?
Related
I am working on a Django app and have deployed it to a Ubuntu Server via AWS. Everything that doesn't require a database insert works fine. But when I try to log in (everytime if someone logs in I am saving the 'last login') an OperationalError appears: Attempt to write a readonly.
I already changed the owner of the database and tried also with chmod 666 and chmod 777 but it still doesn't work.
Did someone had the same issue and has solved it?
Kind regards
I found it! Stupid mistake -> I didn't set the permissions on the parent folder of database.
I have read the answers to similar (same) questions and the solutions have not worked for me.
I have a django application running with Apache2 webserver, and the user is set to the default www-data. Currently, www-data has full access to the db. This was working fine until couple of commits ago and I'm clueless at exactly what went wrong (Since I didn't touch the database at all during the period). Another thing to note is that makemigrations work perfectly fine, and the database is editable without errors in the django admin console.
Currently, this is my permission settings for the db:
The error message is:
Any help would be appreciated thanks!
After updating the django settings.py default database to correct values.
i am able to run makemigrations and migrate command and also create super user. but when i login to the admin it gives me error of “no such table - auth user, OPERATIONAL ERROR”.
I noticed that even if i delete db.sqlite3 file, it comes back when i try to login, i think django looks for table in db.sqlite3 and not postgres.
why db.sqlite3 file re appear after deleting ?
how do i correctly configure my settings.py ?
i am integration using digitalocean managed database services with django installed in one droplet, i have integrated both preciously without error but i installed postgres, this is the first time using managed database service.
Thanks
It seems that your django settings are still pointing to the SQlite database.
Did you reload your WSGI process ? If not, the old SQlite settings are still used in memory.
I know there are a lot of questions floating around there relating to similar issues, but I think I have a specific flavor which hasn't been addressed yet. I'm attempting to create my local postgresql database so that I can do local development in addition to pushing to Heroku.
I have found basic answers on how to do this, for example (which I think is a wee bit outdated):
'#DATABASES = {'default': dj_database_url.config(default='postgres://fooname:barpass#localhost/dbname')}'
This solves the "ENGINE" is not configured error. However, when I run 'python manage.py syncdb' I get the following error:
'OperationalError: FATAL: password authentication failed for user "foo"
FATAL: password authentication failed for user "foo"'
This happens for all conceivable combinations of username/pass. So my ubuntu username/pass, my heroku username/pass, etc. Also this happens if I just try to take out the Heroku component and build it locally as if I was using postgresql while following the tutorial. Since I don't have a database yet, what the heck do those username/pass values refer to? Is the problem exactly that, that I need to create a database first? If so how?
As a side note I know I could get the db from heroku using the process outlined here: Should I have my Postgres directory right next to my project folder? If so, how?
But assuming I were to do so, where would the new db live, how would django know how to access it, and would I have the same user/pass problems?
Thanks a bunch.
Assuming you have postgres installed, connect via pgadmin or psql and create a new user. Then create a new database and with your new user as the owner. Make sure you can connect via psql with the new user into to the database. you will then need to set up an env variable in your postactivate file in your virtualenv's bin folder and save it. Here is what I have for the database:
export DATABASE_URL='postgres://{{username}}:{{password}}#localhost:5432/{{database}}'
Just a note: adding this value to your postactivate doesn't do anything. The file is not run upon saving. You will either need to run this at the $ prompt, or simply deactivate and active your virtualenv.
Your settings.py should read from this env var:
DATABASES = {'default': dj_database_url.config()}
You will then configure Heroku with their CLI tool to use your production database when deployed. Something like:
heroku config:set DATABASE_URL={{production value here}}
(if you don't have Heroku's CLI tool installed, you need to do it)
If you need to figure how exactly what that value you need for your production database, you can get it by logging into heroku's postgresql subdomain (at the time this is being written, it's https://postgres.heroku.com/) and selecting the db from the list and looking at the "Connection Settings : URL" value.
This way your same settings.py value will work for both local and production and you keep your usernames/passwords out of version control. They are just env config values.
I am learning to user PostgreSQL and Django together in Ubuntu 11.10, and I have found that I need to switch to the user I created when I installed PostgreSQL ("postgres") in the Terminal (via "sudo su postgres") in order to create and then access databases to work with in Django. Unfortunately, the "postgres" user doesn't have file writing privileges on my local file system, so when I try to do certain things like add model objects to the database that have an image field, I'm blocked. But if I switch to my normal Ubuntu user name in the Terminal and try to access my admin site on Django's dev server, I get an error like this:
OperationalError at /admin/
FATAL: Peer authentication failed for user "postgres"
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.3.1
Exception Type: OperationalError
Exception Value:
FATAL: Peer authentication failed for user "postgres"
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py in _cursor, line 140
Python Executable: /usr/bin/python
Python Version: 2.7.2
I'm trying to read up on the PostgreSQL docs to give my normal user access to the databases I've created for my Django projects, but I can't figure it out because I don't understand the relationship between PostgreSQL roles/users and Ubuntu users. Could someone please explain to me how I can give my normal Ubuntu username access to my databases in PostgreSQL? I will probably need a specific list of instructions as I've tried to piece it together using the PostgreSQL docs and I'm totally lost.
cfedermann was correct; this is a duplicate of the question at:
Django connection to PostgreSQL: "Peer authentication failed"
and the solution is the same. In the 'settings.py' file for my Django project, under the DATABASES section, my 'HOST' was set to an empty string because there is a built-in comment there (meaning, a comment written by a Django developer) that literally says, 'Set to empty string for localhost.' Apparently this isn't true, as only after I explicitly entered 'localhost' was I able to connect to my psql database under my default Linux username.
I guess the lesson here is, to all you Django other newbies out there trying to use it with postgresql in development, make sure you enter in 'LOCALHOST' in your 'settings.py' file, or you won't be able to connect to a psql database stored on your own machine!
Thanks again cfedermann for pointing me to the answer.
Postgres ident notes
Your normal user may not be allowed to login from local host. If your django project is running on the same machine as your PG database then you need to check the pg_ident.conf file.
Basically you can set postgres so that your unix user is automatically mapped to a postgre user so when you run pgsql it attempts to log you in. You can block a user from logging in on an external IP, or from local host. So check these settings first before fiddling with your Django settings.
ELSE try this
okay you should probably do this
sudo passwd postgres
Enter a password for the unix postgres user then
su postgres
psql
create database djangodb;
create role djangouser with password 'something';
Try those user details in your settings.py file. More on postgres here
ALSO
Ident methods and The pg_hba.conf file
This helps you yes?