PostgreSQL can't create user [duplicate] - postgresql

This question already has answers here:
In psql, why do some commands have no effect?
(2 answers)
Closed 3 years ago.
Issue: I can create users or databases from the shell (bash, OSX) but not postgres cli. From bash I get no confirmation if successful.
If I try to CREATE ROLE in psql then I get no response and it doesn't generate any error. If I try to createuser from bash then if successful it reports back nothing, if unsuccessful then it does generate the error: "role username already exists".
Example:
Yunti-# CREATE ROLE testuser
Yunti-# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
Yunti | Superuser, Create role, Create DB, Replication | {}
anything | | {}
monkey | | {}
Yunti-# CREATE DATABASE testdb
Yunti-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
Yunti | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/Yunti +
| | | | | Yunti=CTc/Yunti
template1 | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/Yunti +
| | | | | Yunti=CTc/Yunti
test | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
test5 | Yunti | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(6 rows)
Yunti-#
A similar thing happens when using createdb.
How can I create users and databases in postgres cli?
And is this normal to get no response to most postgres commands in bash?
Info: users and their privileges:
Yunti-# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
Yunti | Superuser, Create role, Create DB, Replication | {}
anything | | {}
monkey | | {}

Your statements are not executed, because you don't terminate them properly using a ;.
Quote from the manual:
A command is composed of a sequence of tokens, terminated by a semicolon (";").
And in the manual for psql:
At the prompt, the user can type in SQL commands. Ordinarily, input lines are sent to the server when a command-terminating semicolon is reached. An end of line does not terminate a command. Thus commands can be spread over several lines for clarity. If the command was sent and executed without error, the results of the command are displayed on the screen.
If you do that, you get an output like this:
psql (9.4.4)
Type "help" for help.
postgres=# CREATE ROLE testuser;
CREATE ROLE ---<<<< this tells you the statement was executed
postgres=#

I dont know if the postgres client is using other commands in OSX as it is in Linux but I assume it is the same.
This docs link shows some options for the postgres client:
It seems like "\l" lists the databases while the option you would like to see is roles and their access which is "\du".
When creating a database from within the client you should get a response in the form of "CREATING DATABASE". Maybe you are having some sort of syntax error?
I don't think users is created elsewhere.
I hope this solves some of your problems.

Related

Postgresql error database does not exist however it exists when listing the databases

When I drop the database I have an error that it doesn't exist however when I list the databases I can see it.
Here are the steps I am following:
sudo -u postgres psql
postgres=# \l
postgres=# DROP DATABASE IF EXISTS mydbname;
NOTICE: database "mydbname" does not exist, skipping
DROP DATABASE
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------------+---------------+----------+---------+---------+----------------------------
mydbname | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres +
| | | | | cryptouser=CTc/postgres

relation *db_name* does not exist postgres

I am trying to set up an ubuntu server with postgres(following digitalocean tuorial).
I ssh'd to the server as root user, from there created a user named 'justin', gave that user admin privileges as instructed. Switched to 'justin', installed python, django, pip, etc and postgres. From here the tutorial said type sudo -u postgres psql
, I did this. From here I created a db 'jobzumodb' and user 'jobzumojustin'. I am now trying to grant privileges of that db to this new user: GRANT ALL PRIVILEGES ON jobzumodb TO jobzumojustin; and it returns ERROR: relation "jobzumodb" does not exist. However if I postgres=# \l I see:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+---------+-----------------------
jobzumodb | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | admin=CTc/postgres
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
Anyone know what is causing this?
Note: when I created the db I created it with: CREATE DATABASE jobzumoDB;. And was trying to access it for a long time as 'jobzumoDB' and not 'jobzumodb', before learning the \l command and that it saved as 'jobzumodb' because I did not pass it in quotes. Anyway, not sure if this could be having an effect.
Thanks for any help.
Another edit: upon rereading this I realized I am using the 'postgres' user (I see postgres=#), should I be using the 'jobzumojustin' user and jobzumojustin=# ? Also, I checked to see if 'jobzumojustin' exists, it does, but does not have anything listed under 'list of roles and attributes'.
I think you forgot the database keyword. It should be:
GRANT ALL PRIVILEGES ON DATABASE jobzumodb TO jobzumojustin;

POSTGRESQL : Old owner present on new installation

I want to flush this DB so that the old owner doesn't exist anymore and I want to be the superuser.
I am using an ex-colleagues laptop ( mac )
PostgreSQL ( psql ) was installed via Homebrew ( 9.6 ) which I have removed as I required ( 9.5 ).
A few "perhaps" issues I've noticed:
psql9.5 is now installed and when I attempt to login I receive the following FATAL: role "MY NAME" does not exist
eventually login and when I "\l" I see the following:
DB_NAME-> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+--------------+----------+-------------+-------------+-------------------------------
DB_NAME | DB_NAME | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
postgres | EX COLLEAGUE | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
template0 | EX COLLEAGUE | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/EX COLLEAGUE +
| | | | | EX COLLEAGUE=CTc/EX COLLEAGUE
template1 | EX COLLEAGUE | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/EX COLLEAGUE +
| | | | | EX COLLEAGUE=CTc/EX COLLEAGUE
when I run:
DB_NAME=> \du
List of roles
Role name | Attributes | Member of
--------------+------------------------------------------------------------+-----------
DB_NAME | Create DB | {}
EX COLLEAGUE | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
I can't create new Roles.
I might be over-exaggerating the severity of this - but I have no idea what to do.
I have looked at old resources and maybe I was searching incorrectly but I found no help.
Solved - had to re-install my Mac OS but was hoping for a less time consuming option

How to import database to PostgreSQL as root?

I want to import a database of mine to a database of my server. So, I copied my database dump file to my server's root directory and logged in and did this:
root#iWidgetServer1:~# sudo -u postgres psql -U iwidget -d iwidget -f iwidget_dump2.sql
could not change directory to "/root"
psql: FATAL: Peer authentication failed for user "iwidget"
However, iwidget is a role and has granted all priviliges for this database:
root#iWidgetServer1:~# sudo -u postgres psql -l
could not change directory to "/root"
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------------+----------+----------+-------------+-------------+-----------------------
iwidget | iwidget | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =Tc/iwidget +
| | | | | iwidget=CTc/iwidget
postgres | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
sample_db | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
template0 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template_postgis | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 |
(6 rows)
What am I doing wrong?
From the docs:
The peer authentication method works by obtaining the client's operating system user name from the kernel and using it as the allowed database user name (with optional user name mapping). This method is only supported on local connections.
You're doing sudo -u postgres, but are trying to connect as iwidget.
You need to create a user named iwidget and login as this user.
This seems to be more a problem with the File permissions of the dump instead of Postgres. Have you tried moving the dump to a folder not owned by root?

PostgreSQL: Drop Database but DB is still there [duplicate]

This question already has answers here:
In psql, why do some commands have no effect?
(2 answers)
Closed 2 years ago.
I am new to PostgreSQL and I try to get my head around it. I am familiar to db's and MySQL.
I am trying to delete database, which I created since psql seems to ignore the changes I try to push through Django.
When I execute \l I get the following response:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------------+--------+----------+-------------+-------------+-------------------
postgres | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 |
test_db | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 |
template0 | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 | =c/neurix +
| | | | | neurix=CTc/neurix
template1 | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 | =c/neurix +
| | | | | neurix=CTc/neurix
template_postgis | neurix | UTF8 | en_AU.UTF-8 | en_AU.UTF-8 |
(5 rows)
Now I wan to drop the database "test_db" with
DROP DATABASE test_db
but when I execute \l afterwards, the table is still there and the overview looks like about.
Did you type a ; after the DROP DATABASE test_db? Did PostgreSQL print a response to your command?
I had a similar issue when working on a Rails 6 application in Ubuntu 20.04 with PostgreSQL as my database.
When I run the command:
DROP DATABASE my-db;
The database is dropped successfully, however, the schema for the database is still left.
So when I run the command:
CREATE DATABASE my-db;
And I check the tables in the newly created database, I realized they still contained the same tables as the previously deleted database, even though I have not run any migration.
Here's how I fixed it:
Instead of running the command:
DROP DATABASE my-db;
run the command:
DROP DATABASE IF EXISTS my-db;
This deletes the database and it's corresponding schema.
That's all.
I hope this helps