0

I am working on connecting a Postgres DB to Django. I have created the db through pgAdmin 4 under the user postgres. I've set my environment variables as followed

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
try:
    if sys.argv[1:2] != ['test']:
        DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.postgresql',
                'HOST': env('PGHOST'),
                'USER': env('PGUSER'),
                'NAME': env('PGDATABASE'),
                'PASSWORD': env('PGPASSFILE'),
                'PORT': env('PGPORT'),
            }
        }
except:
    pass

with my .env file containing each variable. When I try to migrate it through ubuntu command line I get the error database <dbname> does not exist I've installed psycopg2 and pillow and set local all postgres peer to local all postgres md5. my os is windows 11 so I am wondering if this is happening because my project directory is through ~/directory/to/path and postgres and pgAdmin are installed in /etc/postgresql/ and /mnt/c/'Program Files'/PostgreSQL? Should I move those files from / to ~?

NOTE: I've run psql canvasDB and got back role "awilbur" does not exist

1 Answer 1

1

If you installed postgresql through an installer (which I assume is the way since you're on Windows), you might've remembered that the installer asked you for a password. If you do remember, you should try:

me@My-MacBook-Pro ~ [2]> psql -U postgres
Password for user postgres: 
psql (14.2, server 14.1)
Type "help" for help.

postgres=# CREATE DATABASE test;
CREATE DATABASE

Specifying -U postgres will log you into the default user, whose default password is the password you entered into the installer.

If you don't, you could try this: go to the Control Panel -> Administrative Tools -> Computer Management. Under "Local Users and Groups" you can see all users created for your system. Remove "postgres" and reinstall PostgreSQL.

After you're done, you can verify by:

postgres=# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 test      | postgres | UTF8     | C       | C     | 
(5 rows)
Sign up to request clarification or add additional context in comments.

4 Comments

So that is creating the database through my command line, but it's not getting linked to pgAdmin-4. Any ideas as to why?
As far as my own project goes, I don't think you need to tie the database to pgAdmin. Can you create the database in the first place?
yeah, that's not a problem. I don't need pgAdmin but wanted to try it out simply for learning and visual purposes. As long as the database is getting created though I suppose it doesn't matter. Thanks!
so the database is being created when I run psql -U postgres CREATE DATABASE \l but it's not letting me migrate in django. It keeps giving the same error as I mentioned earlier. Any thoughts?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.