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.
Related
So initially, I deployed a nestjs application to elasticbeanstalk with docker-compose. The application was working perfectly with the configured elasticbeanstalk RDS database, at the same time running well with a remote mongodb atlas cluster. But after adding a new feature to my code (this feature has nothing to do with the database configurations), and eb deployed, the elastic beanstalk application just stopped working.
It gave this error:
| [31m[Nest] 1 - [39m07/03/2022, 6:04:51 PM [31m ERROR[39m [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (7)...[39m
app_name | [31m[Nest] 1 - [39m07/03/2022, 6:04:54 PM [31m ERROR[39m [38;5;3m[MongooseModule] [39m[31mUnable to connect to the database. Retrying (8)...[39m
app_name | [31m[Nest] 1 - [39m07/03/2022, 6:04:54 PM [31m ERROR[39m [38;5;3m[TypeOrmModule] [39m[31mUnable to connect to the database. Retrying (8)...[39m
app_name | [31m[Nest] 1 - [39m07/03/2022, 6:04:57 PM [31m ERROR[39m [38;5;3m[ExceptionHandler] [39m[31mThe `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.[39m
app_name | at __awaiter (/usr/src/app/node_modules/#nestjs/mongoose/dist/mongoose-core.module.js:16:12)
app_name | at Function.<anonymous> (/usr/src/app/node_modules/#nestjs/mongoose/dist/mongoose-core.module.js:60:69)
app_name | at Generator.next (<anonymous>)
app_name | at Mongoose.createConnection (/usr/src/app/node_modules/mongoose/lib/index.js:286:10)
app_name | at NativeConnection.Connection.openUri (/usr/src/app/node_modules/mongoose/lib/connection.js:689:11)
app_name | at new Promise (<anonymous>)
app_name | at Observable._subscribe (/usr/src/app/node_modules/rxjs/dist/cjs/internal/observable/defer.js:8:31)
app_name | at Observable._trySubscribe (/usr/src/app/node_modules/rxjs/dist/cjs/internal/Observable.js:41:25)
app_name | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1195:16)
app_name | at /usr/src/app/node_modules/#nestjs/mongoose/dist/mongoose-core.module.js:20:71
app_name | at /usr/src/app/node_modules/#nestjs/mongoose/dist/mongoose-core.module.js:59:80
app_name | Error: connect ECONNREFUSED 127.0.0.1:5432
app_name | MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
current_app_name exited with code 1
This is my database configurations for the nestjs application:
// app.module.ts file.
#Module({
imports: [
// MONGO_DATABASE_URI here is the connection string to a remote mongodb atlas cluster
MongooseModule.forRoot(process.env.MONGO_DATABASE_URI),
TypeOrmModule.forRoot({
type: 'postgres',
// these environment variables are related to AWS RDS variables
host: process.env.RDS_HOSTNAME, .
port: parseInt(process.env.RDS_PORT),
username: process.env.RDS_USERNAME,
password: process.env.RDS_PASSWORD,
database: process.env.RDS_DB_NAME,
entities: [<My Entities here>],
synchronize: true,
}),
PLease I need help.
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)
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 have a Django 1.5 application running on apache2 server and using sqlite database.
The setting for the database is as follow
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '/var/www/html/project/devdb',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
where devdb is the database file generated.
When I run migration commands from the terminal console, it works fine and data is migrated to the database.
But when I visit the application URL, it gives the error
DatabaseError at /url_path/
attempt to write a readonly database
Then I change the permission of file using
sudo chown 777 devdb
Then it starts giving the error
DatabaseError at /url_path/
unable to open database file
ls -la gives following output for the devdb file
-rwxr-xr-x 1 ubuntu ubuntu 1036 May 11 2018 manage.py
-rw-rw-r-- 1 ubuntu ubuntu 291840 May 28 09:31 devdb
Getting user of the apache2 running using
ps -ef | egrep '(httpd|apache2|apache)' | grep -v `whoami` | grep -v root | head -n1 | awk '{print $1}'
gives www-data
But changing owner of the file to www-data again gives unable to open database file.
A sqlite db needs to be owned by www-data and so does the directory it is in.
So if your app is in /var/www/app do
cd /var/www
chown www-data:www-data app
cd app
chown ww-data:www-data devdb
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?