I am using django-extensions reset_db command and get the following error:
psycopg2.ProgrammingError: database "database_name" already exists
I've verified that my user has CreateDB and Login privileges. All packages are up to date. How can I tell why it's not able to reset the database correctly?
It turned out my user was not the owner of the database - the postgres superuser was.
I changed the owner of the database to my Django user and the reset_db command worked:
ALTER DATABASE database_name OWNER TO owner_name;
Related
I am trying to migrate to a PostgreSQL db in RDS from Django. The db was set up successfully but when I run python3 manage.py migrate I keep getting the error 'FATAL: password authentication failed for user'. The user name that it mentions is not the master username from RDS which is the same as my USER in settings. I have a .env file with the USER, ENGINE, PASSWORD etc that is being read correctly apart from the fact that it is not referencing the USER. It is referencing the username I use to log onto the computer which I also used when I set up a superuser in the venv.
I have tried logging out from superuser which did nothing. I also tried deactivating the venv which seemed to solve the authentication issue though obviously none of the modules within the venv uploaded so I got errors from that.
I tried changing the password in RDS but it is still trying to connect using the wrong user. I also tried making a new venv but still got the same error. The NAME in settings is set to postgres.
Has anyone else had this issue?
I am using django-dbbackup to create a backup of my postgres db. When I run python manage.py dbbackup it creates a default.backup file.
But, when I run python manage.py dbrestore I get the following error.
Restoring backup for database: hera
Finding latest backup
Restoring: /home/dev/Documents/Program Codes/Python/Django/Hera/default.backup
Restore tempfile created: 670.5 KB
Are you sure you want to continue? [Y/n]y
Running: dropdb --username=****** --host=localhost hera
dropdb: database removal failed: ERROR: must be owner of database hera
CommandError: Error running: [u'dropdb', u'--username=dev', u'--host=localhost', u'hera']
This is about making sure the user you're using is the owner of the database. Right now it looks like you're running the dbrestore command using the dev user (from the line CommandError: Error running: [u'dropdb', u'--username=dev', u'--host=localhost', u'hera']).
There are likely three options:
1/ Change the owner of the database to dev using a PSQL command something like:
ALTER DATABASE hera OWNER TO dev;
2/ Change the --username=dev your dbrestore script uses when it runs to the owner of the database (something other than dev, you can use \l in PSQL to list your databases and see which user owns the database.
3/ You could give dev superuser and createdb ability, though not sure this is ideal in terms of security.
How to recover a password for my heroku postgresql database? I used this command heroku pg:credentials DATABASE --reset but can't gain access to my django admin.
You can see your current credentials with heroku pg:credentials DATABASE or heroku config. You can then use the elements of the URL displayed to connect via a DB admin tool like Pgadmin. The password is the text between the : and the #.
I installed OSQA using the bitnami installer and everything worked fine. Now, I am hacking at the osqa code base. If I need to restore the database to its initial state, do i need to reinstall OSQA or is there any command to truncate the database and load intial data.
I tried using use_osqa.bat and did a python.py manage migrate forum but it didnt work.
It uses a postgresql database
You can use the django-admin.py flush:
Returns the database to the state it was in immediately after syncdb was executed. This means that all data will be removed from the database, any post-synchronization handlers will be re-executed, and the initial_data fixture will be re-installed.
django-admin.py flush
Finally, this worked for me. Note that this is applicable only for OSQA using the bitnami environment with postgresql database.
if you want to restore your database state to the original version, first connect to postgres database using admin credentials(use any client, I used Dbeaver)
database : postgres
username : postgres
password : **admin password** : this is the same password you gave while installing bitnami-osqa
Now, drop the bitnami-osqa database and create it again(if you already have connections to bitnami_osqa, close them)
Drop-database bitnami_osqa;
Commit;
Create-database bitnami-osqa;
Commit;
Now open use_osqa.bat in your bitnami root folder and execute the following
cd apps\osqa
python manage.py syncdb --all --verbosity 2
[specify yes when it prompts to create super user and create some user]
python manage.py migrate forum --fake
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?