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
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)
Related
I want to use a better database with my Django like postgresql or mysql. what are the steps in doing that?
In case of MySQL:
Install mysqlclient in your virtualenv.
$ pip install mysqlclient
Enter MySQL and Create database to connect your project
$ mysql
mysql> create database django_project;
Query OK, 1 row affected (0.01 sec)
mysql> use django_project
mysql> show tables;
Empty set (0.01 sec)
Connect your project to database(MySQL)
# your_settings.py
...
DATABASES = {
'default' : {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_project',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
}
}
Migrate
$ python manage.py migrate
Check
$ mysql
mysql> use django_project;
mysql> show tables;
+--------------------------+
| Tables_in_django_project |
+--------------------------+
| accounts |
| comments |
| django_content_type |
| django_migrations |
| django_session |
+--------------------------+
5 rows in set (0.01 sec)
Done!
I am trying to access my postgres database but I get a permission denied error. I created database + table (was created in python but it is there):
sudo -u postgres psql
postgres=# create user lukasz1 with encrypted password '<password>';
postgres=# create database mydatabase owner lukasz1;
So far so good:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+----------+----------+-------------+-------------+-----------------------
mydatabase | lukasz1 | UTF8 | pl_PL.UTF-8 | pl_PL.UTF-8 | =Tc/lukasz1 +
| | | | | lukasz1=CTc/lukasz1
postgres | postgres | UTF8 | pl_PL.UTF-8 | pl_PL.UTF-8 |
template0 | postgres | UTF8 | pl_PL.UTF-8 | pl_PL.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | pl_PL.UTF-8 | pl_PL.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
and lukasz1 is an owner of mydatabase.
But when I try to connect it I am get "permission denied".
I can't even select from my table to view it as permission is denied:
mydatabase=> select article_id from articles;
ERROR: permission denied for table articles
I checked multiple answers to similar questions and tried checking user valid_until and got an empty values in that column:
mydatabase=> select usename, valuntil from pg_user;
usename | valuntil
----------+----------
postgres |
lukasz |
lukasz1 |
(3 rows)
In other question I found that pgAdmin4 can bug this and I should change valid until to infinity,which I tried but without any result as permission was denied to make this change.
mydatabase=> alter user lukasz1 valid until 'infinity';
ERROR: permission denied
I checked privileges of the users and got this:
postgres=# select * from pg_user;
usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | useconfig
----------+----------+-------------+----------+---------+--------------+----------+----------+-----------
postgres | 10 | t | t | t | t | ******** | |
lukasz | 16384 | f | f | f | f | ******** | |
lukasz1 | 17256 | f | f | f | f | ******** | |
lukasz2 | 17437 | f | f | f | f | ******** | |
(4 rows)
I turned out that even though lukasz1 is owner of the mydatabase that account can't do anything with databse.
Next thing I checked was default access privileges which was completly empty:
mydatabase=> \ddp
Default access privileges
Owner | Schema | Type | Access privileges
-------+--------+------+-------------------
(0 rows)
I tried changing password but it changed nothing.
ALTER USER lukasz1 WITH PASSWORD '<new password>';
I tried to create public schema but wasn't allowed as permission was denied:
mydatabase=> create schema public;
ERROR: permission denied for database mydatabase
All of that resulted, that docker container with web cannot access container with db as
password authentication failed for user "lukasz1"
I cannot even view hba_file:
mydatabase=> show hba_file;
ERROR: must be superuser or a member of pg_read_all_settings to examine "hba_file"
Can anyone please advice how can I make this work?
EDIT:
I checked the logins and were able to create lukasz as superuser.
usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls
lukasz | 16384 | f | t | f | f
When using his credentials I am able to create tables, insert data into it etc.
psql -d mydatabase -U lukasz -W -c "insert into articles values (1, 'Ćukasz', 'lukasz', 'title','tekst')"
But after creating containers I still get
django.db.utils.OperationalError: FATAL: password authentication failed for user "lukasz"
docker-compose.yml
version: "3.9"
services:
db:
image: postgres:9.6
environment:
- POSTGRES_DB=mydatabase
- POSTGRES_USER=lukasz
- POSTGRES_PASSWORD=password
networks:
- backend
web:
build: .
command: python3 manage.py runserver 127.0.0.1:8080
volumes:
- .:/code
ports:
- "8080:8080"
depends_on:
- db
networks:
- backend
networks:
backend:
driver: bridge
name: backend
setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'lukasz',
'PASSWORD': 'password',
'HOST': 'db',
'PORT': 5432,
}
}
sqlalchemy engine:
engine = create_engine("postgresql://lukasz:password#db:5432/mydatabase")
I'm trying to create a PostgreSQL local db dump via Terminal on macOS using psql. My next step is to upload/import my entire db to my Amazon RDS PostgreSQL db instance.
Can anyone help me with the Terminal command to create a dump file 'mydbdump.sql' for my 'mydb' database. I've sorted through existing questions, searched forums (tried similar Stack Overflow question), even Amazon RDS docs (above, link), and am having no luck with the commands from any of these docs. What am I doing wrong?
Here is what I've tried so far (and I've gotten nothing back, not even an error):
Via sudo:
sudo -u postgres psql pg_dump -Fc -o -f /Users/<my_computer>/database_backup.sql mydb
Via psql, connected to mydb (\c mydb;):
mydb-# pg_dump dbname=mydb -f mydbdump.sql
mydb-# pg_dump -U postgres -W -F t mydb > /Users/<my_computer>/database_backup.sql
mydb-# pg_dump -Fc mydb > /Users/<my_computer>/database_backup.sql
mydb-# pg_dump -U postgres mydb > database_backup.sql
mydb-# pg_dump -Fc mydb > /Users/<my_computer>/database_backup.sql
mydb-# pg_dump -U postgres -W -F t mydb > /Users/<my_computer>/database_backup.sql
For reference:
I'm using Django, in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydb',
'USER': 'dbuser',
'PASSWORD': '<MYPASSWORD>',
'HOST': 'localhost',
'PORT': '',
}
}
By the way, my Django app is running successfully.
When I list all database relations in mydb:
\dt mydb;
Output:
List of relations
Schema | Name | Type | Owner
--------+----------------------------+-------+--------
public | auth_group | table | dbuser
public | auth_group_permissions | table | dbuser
public | auth_permission | table | dbuser
public | auth_user | table | dbuser
public | auth_user_groups | table | dbuser
public | auth_user_user_permissions | table | dbuser
public | django_admin_log | table | dbuser
public | django_content_type | table | dbuser
public | django_migrations | table | dbuser
public | django_session | table | dbuser
public | polls_choice | table | dbuser
public | polls_question | table | dbuser
(12 rows)
When I list all schemas in psql:
select s.nspname as table_schema,
s.oid as schema_id,
u.usename as owner
from pg_catalog.pg_namespace s
join pg_catalog.pg_user u on u.usesysid = s.nspowner
order by table_schema;
Output:
table_schema | schema_id | owner
--------------------+-----------+----------
information_schema | 13337 | postgres
pg_catalog | 11 | postgres
pg_temp_1 | 12314 | postgres
pg_toast | 99 | postgres
pg_toast_temp_1 | 12315 | postgres
public | 2200 | postgres
First running pg_dump inside psql will not work.
Second, this:
sudo -u postgres psql pg_dump -Fc -o -f /Users/<my_computer>/database_backup.sql mydb
is a variation of the first thing. You want:
sudo -u postgres pg_dump -Fc -f /Users/<my_computer>/database_backup.sql mydb
pg_dump is a client program by itself.
NOTE: No -o. There is -O which is for no-owner.
See examples at bottom of this page:
https://www.postgresql.org/docs/current/app-pgdump.html
Lastly are you sure this doesn't get you an error?:
test(5432)=# pg_dump dbname=mydb -f mydbdump.sql;
ERROR: syntax error at or near "pg_dump"
LINE 1: pg_dump dbname=mydb -f mydbdump.sql;
If not then I would check your logging settings in postgresql.conf.
This is the command that finally worked:
sudo -u postgres pg_dump mydb > db.sql
This prompted me for:
my computer password
then my postgres password
I did not need to specify any additional paths. The db.sql file automatically saved to my root directory.
TL;DR manage migrate fails but manage dbshell works.
This is a dockerized dev environment on OSX10.6, boot2docker with django 1.8.4 and postgresql 9.4.4.
/manage.py migrate throws:
django.db.utils.OperationalError: FATAL: password authentication failed for user "xxxxx"
but here's the bit that confuses me, django dbshell works, copy and pasting the password out of the environment variable
root#df6501ab7341:/src/web# ./manage.py dbshell
Password for user xxxxx:
psql (9.4.4)
Type "help" for help.
xxxxx=# \du
List of roles
Role name | Attributes | Member of
--------------+------------------------------------------------+-----------
xxxxx | Superuser +| {}
| Password valid until infinity |
mpasysuser01 | | {}
postgres | Superuser, Create role, Create DB, Replication | {}
xxxxx=#
Django settings are:
# Database Configuration
DATABASES = {
'default': {
'ENGINE' : 'django.db.backends.postgresql_psycopg2',
'NAME' : os.environ.get('DATABASE_ENV_POSTGRES_USER' , ''), # the database name is the same as the dbo username
#ToDo Use unprivileged database user
# 'USER' : os.environ.get('DATABASE_ENV_UNPRIVILEGED_SQL_USER', ''),
'USER' : os.environ.get('DATABASE_ENV_POSTGRES_USER', ''),
'PASSWORD' : os.environ.get('DATABASE_ENV_POSTGRES_PASSWORD', ''),
'HOST' : os.environ.get('DATABASE_PORT_5432_TCP_ADDR' , ''),
'PORT' : os.environ.get('DATABASE_PORT_5432_TCP_PORT' , ''),
},
}
and the database logs show:
database_1 | FATAL: password authentication failed for user "xxxxx"
database_1 | DETAIL: Connection matched pg_hba.conf line 95: "host all all 0.0.0.0/0 md5"
I second what #larconda said.
I was racking my brain and decided to print by:
Open the shell
import os
print(your variable here)
That should provide some input - it turned out to be a missing space for me.
I switched to my database engine to PostGIS and now I'm switching back to PostgreSQL (so I can continue to use Heroku for free). I changed my settings.py file back to:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'chishenma',
'HOST': 'localhost',
}
}
and commented out the installed app so now it shows:
# 'django.contrib.gis',
First I made sure that Postgres.app is running. Then I deleted my migrations, ran python manage.py syncdb, and did the South migrations again, all without errors. When I go into the psql shell and type \l, I can see my database, "chishenma," in the list:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-------------------------+-----------------+----------+-------------+-------------+----------------------
chishenma | michelleglauser | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
However, when I type \d, I get "No relations found." I tried to re-create the database, but I get "ERROR: Database chishenma already exists."
I looked at this and this Stack Overflow thread and determined that my access privileges (I've got the "=UC/postgres" part) and search_path are correct. I get:
List of schemas
Name | Owner | Access privileges | Description
--------+-----------------+------------------------------------+------------------------
public | michelleglauser | michelleglauser=UC/michelleglauser+| standard public schema
| | =UC/michelleglauser
and:
List of roles
Role name | Attributes | Member of
-----------------+------------------------------------------------+-----------
michelleglauser | Superuser, Create role, Create DB, Replication | {}
What am I missing? How do I make the relations findable?