Question

postgresql - cannot change "postgres" user

My postgres databse keeps the value postgres for NAME, USER, PASSWORD even when I create new db and new user. It seems that no matter what I do, I cannot alter it.

I use django and an ubuntu droplet.

Steps to reproduce: I add new user, give it admin rights, ensure OpenSSH is open.

Logged as the new user and:

Log into a postgres session-           `sudo -u postgres psql`
create new db:                     `CREATE DATABASE new_db;`
Create new user with a password        `CREATE USER dbuser WITH PASSWORD 'dbpsw';`
Alter the roles and grant it all priviliges. 

verify connection:

\c new_db

Yet, if I go the bash and retreive the values of the postgres database, Yet, my database stays the same:

NAME = postgres
Password = postgres
User = postgres

>>> DATABASES
{'default': {'ENGINE': 'django.db.backends.postgresql', 'NAME': 'postgres', 'USER': 'postgres', 'PASSWORD': 'postgres', 'HOST': 'localhost', 'PORT': 5432, 'ATOMIC_REQUESTS': False, 'AUTOCOMMIT': True, 'CONN_MAX_AGE': 0, 'CONN_HEALTH_CHECKS': False, 'OPTIONS': {}, 'TIME_ZONE': None, 'TEST': {'CHARSET': None, 'COLLATION': None, 'MIGRATE': True, 'MIRROR': None, 'NAME': None}}}

Any advice as how I can modify and enforce the new databse and user would be highly appreciated.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
May 9, 2023

Hi there,

It sounds like that the issue you’re seeing is related to the Django settings file itself rather than the Postgres configuration. The Django settings file might still contains the default configuration for the PostgreSQL database. You should update your Django settings to use the new database and user you created.

To do that, open your Django project’s settings file, typically found in my_project/settings.py. Locate the DATABASES dictionary and change the values accordingly:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'new_db',               # Change this to your new database name
        'USER': 'dbuser',               # Change this to your new user
        'PASSWORD': 'dbpsw',            # Change this to your new user's password
        'HOST': 'localhost',
        'PORT': 5432,
        ... # the rest of your settings

After updating the settings file, you should restart your Django development server to apply the changes. If you’re using a production server like Gunicorn or uWSGI, you’ll need to restart the server as well.

After updating the settings and restarting the server, Django should use the new database and user for all database operations.

Let me know how it goes!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel