Postgres - can't acces database, no acces - django

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")

Related

database does not exist postgresql

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)

Creating a PostgreSQL database backup via psql on macOS

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.

Problem App Engine app to connect to MySQL in CloudSQL

I've configured SQL second gen. instance and App Engine application (Python 2.7) in one project. I've made necessary settings according to that page.
app.yaml
runtime: python27
api_version: 1
threadsafe: true
env_variables:
CLOUDSQL_CONNECTION_NAME: coral-heuristic-215610:us-central1:db-basic-1
CLOUDSQL_USER: root
CLOUDSQL_PASSWORD: xxxxxxxxx
beta_settings:
cloud_sql_instances: coral-heuristic-215610:us-central1:db-basic-1
libraries:
- name: lxml
version: latest
- name: MySQLdb
version: latest
handlers:
- url: /main
script: main.app
Now as I try to connect from the app (inside Cloud Shell), the error:
OperationalError: (2002, 'Can\'t connect to local MySQL server through socket \'/var/run/mysqld/mysqld.sock\' (2 "No such file or directory")')
Direct connection works:
$ gcloud sql connect db-basic-1 --user=root
was successful...
MySQL [correction_dict]> SHOW PROCESSLIST;
+--------+------+----------------------+-----------------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+------+----------------------+-----------------+---------+------+----------+------------------+
| 9 | root | localhost | NULL | Sleep | 4 | | NULL |
| 10 | root | localhost | NULL | Sleep | 4 | | NULL |
| 112306 | root | 35.204.173.246:59210 | correction_dict | Query | 0 | starting | SHOW PROCESSLIST |
| 112357 | root | localhost | NULL | Sleep | 4 | | NULL |
| 112368 | root | localhost | NULL | Sleep | 0 | | NULL |
+--------+------+----------------------+-----------------+---------+------+----------+------------------+
I've authorized IP to connect to the Cloud SQL instance:
Any hints, help?
Google AppEngine Standard provides a unix socket at /cloudsql/[INSTANCE_CONNECTION_NAME] that automatically connects you to your CloudSQL instance. All you need to do is connect to it at that address. For the MySQLDb library, that looks like this:
db = MySQLdb.connect(
unix_socket=cloudsql_unix_socket,
user=CLOUDSQL_USER,
passwd=CLOUDSQL_PASSWORD)
(If you are running AppEngine Flexible, connecting is different and can be found here)

rails 4 mysql2 gem Incorrect MySQL client library version! This gem was compiled for 5.5.30 but the client library is 5.6.19

upon deployment in production, I get this error , I don't understand where is coming from this 5.5.30... but I uninstalled the gem locally (oSX) and remotely (Debian) and reinstalled it... so it should be compiled with the latest libraries.. 5.6.19
here are both MySQL versions installed ...
on Debian
mysql -u root -p -e 'SHOW VARIABLES LIKE "%version%";'
Enter password:
+-------------------------+-------------------+
| Variable_name | Value |
+-------------------------+-------------------+
| innodb_version | 5.6.19 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.6.19-1~dotdeb.1 |
| version_comment | (Debian) |
| version_compile_machine | x86_64 |
| version_compile_os | debian-linux-gnu |
+-------------------------+-------------------+
on OSX
yves$ mysql -u root -p -e 'SHOW VARIABLES LIKE "%version%";'
Enter password:
+-------------------------+------------------------------+
| Variable_name | Value |
+-------------------------+------------------------------+
| innodb_version | 5.6.19 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.6.19 |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86_64 |
| version_compile_os | osx10.7 |
+-------------------------+------------------------------+

Switching Django App Back to PostgreSQL, No Relations Found

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?