Becoming a superuser postgresql - postgresql

How do i make myself a superuser on postgresql?
I keep trying to create a database and I keep getting the following errors:
createdb: database creation failed: ERROR: permission denied to create database
I'm guessing this is because i'm not a superuser but i don't know what's wrong.
Here's what i did:
> sudo su postgres
> postgres#precise64:/xxx/xxx/projectfile$createuser -P
> Enter name of role to add: vagrantUser
> Enter password:
> Enter it again:
> Shall the new role be a superuser? (y/n) y
> postgres#precise64:/vagrant/django_projects/mblog$ psql -U vagrantUser template1
> psql: FATAL: Peer authentication failed for user "vagrantUser"
> postgres#precise64:/vagrant/django_projects/mblog$ exit
> (venv)vagrant#precise64:/vagrant/project$:psql template1
> template1 =# \q
> (venv)vagrant#precise64:/vagrant/project$:createdb mydb
I get the following error:
createdb: database creation failed: ERROR: permission denied to create database
Pleae help

Long story short - you don't want vagrant to be superuser. instead, make the vagrant database from postgres account, and make it's owner (i.e. the database) vagrant user.

The createuser script takes a "--createdb" option to allow the new user to create databases (see http://www.postgresql.org/docs/8.1/static/app-createuser.html).
You did not specify this, so "vagrantUser" cannot create new databases.
Having said that, #depesz makes a good point - it's best practice to have a single superuser/root database account and use that to create/maintain databases, create separate accounts for databases and grant appropriate permissions to those accounts; but I was totally focused on the problem =)

Related

How can I create a Postgres 11 user/role with a different name than the OS-user?

I'm a newbie to PostgreSQL and I'm trying to create a user/role on Postgres 11 (running on CentOS7) which is named (e.g.) "json_docs_admin" (same for password) that I can use under the OS user (e.g.) "app_user" to connect locally to the DB.
I have tried connecting to Postgres using "postgres" user and the command "psql -d template1" and ran the following commands:
template1=# create role json_docs_admin with login password 'json_docs_admin';
CREATE ROLE
template1=# create database json_docs owner=json_docs_admin;
CREATE DATABASE
template1=# GRANT ALL PRIVILEGES ON DATABASE json_docs TO json_docs_admin;
GRANT
I have changed the /var/lib/pgsql/11/data/pg_hba.conf so that local connections can be established on the server, and I've restarted the postgresql service.
Then, back under "app_user" user, when I try to connect to the database, I get the following errors:
$psql -d json_docs -U json_docs_admin
Password for user json_docs_admin:
psql: FATAL: database "json_docs" does not exist
Could anybody let me know what am I doing wrong?
Thanks.

createdb: permission denied to create database

I'm trying to set up a Heroku environment for python development following instructions on https://github.com/heroku/python-getting-started. When I run createdb python_getting_started:
I'm first prompted to give in a password: I entered the password of the user "postgres" in Postgres
I get an error message:
createdb database creation failed: ERROR: permission denied to create database
Don't really how to solve this one. The user "postgres" is allowed to create a database. I checked with \du that it is a Superuser and it has Create DB rights. What's going on here? Which user is Windows using to try to create a Postgres DB?
Most PostgreSQL utilities by default use your current OS session login for database connections.
You need to either set environment variable PGUSER to postgres or use createdb -U postgres python_getting_started.
You can read more about createdb parameters here, tho admittedly it does not mention default values.
EDIT: It actually does mention that it uses libpq defaults, and those are:
user
PostgreSQL user name to connect as. Defaults to be the same as the operating system name of the user running the application.

Export everything from PostgreSQL

I want to export everything(users, roles, object definitions, data) from my PostgreSQL and restore it into another server. How can I do that?
I try to use
pg_dumpall -U postgres -g > out.sql
but file out.sql is empty. Can someone explain me why this happens?
When I try:
pg_dumpall -U postgres > out.sql
PostgreSQL just didn't want to accept password for "postgres".
I prefer to do this job from (windows) command line(it is not problem and with linux command, I can translate them). And if there is nice pgAdmin way to do it, I will happy to learn it, too.
P.S: My password for "postgres" is correct (I try to login with "psql -U postgres" and there is no problem)
Update: I try with user different than "postgres"(in this case rsmn):
pg_dumpall -U rsmn > out.sql
I have following error:
pg_dumpall: query failed: ERROR: permission dneied for relation
pg_authid pg_dumpall: query was: SELECT oid, rolname, rolsuper,
rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, rolconnlimit,
rolpassword, rolvalidunil, rolreplication, rolbypassrls,
pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, rolname
= current_user as is_current_user FROM pg_authid WHERE rolname !~ '^pg_' ORDER BY 2
User privileges(This user is only one beside "postgres"):
The attempt with user rsmn fails because it is not a superuser and cannot export the user passwords.
It remains to figure out why pg_dumpall -U postgres fails for you.
For that, the first thing would be to check the PostgreSQL server log. I am not sure where that is on your system, you should check the settings in postgresql.conf (logging_collector, log_destination, log_directory, log_filename). Probably it is in the Windows Event Log.
The log message will tell you what is wrong.
It could well be a problem with the pg_hba.conf file – if so, modern PostgreSQL versions will tell you which line was used.
For debugging purposes, you can try to change the relevant line to trust authentication so that no password is requested (but don't leave it that way, or everybody can connect as user postgres without password!).
If you are trying to do it under the *nix and your server is installed localy, you may not to use ident mode instead of password:
sudo su postgres
pg_dumpall > out.sql

createdb: database creation failed: ERROR: permission denied to create database

I am pretty much confused about root user,super user,user and permissions! I am not able to create a database inside user "athleticu". Following are the commands I used:-
athleticu#ip-172-30-4-103:/home/ubuntu$ createdb -T template0 simple_db1
createdb: database creation failed: ERROR: permission denied to create database
athleticu#ip-172-30-4-103:/home/ubuntu$ sudo createdb -T template0 simple_db1
sudo: unable to resolve host ip-172-30-4-103
createdb: could not connect to database template1: FATAL: role "root" does not exist
Please somebody clarify my doubts and tell me what should I write!
Hey I have already solved this. What you have to do is to first login as postgres user as follows:
$ su postgres
$ psql
postgres=# alter user athleticu createdb;
ALTER ROLE
Hope it helps you :)
Type \du in psql and you will see a list of all the registered users and what type of privileges each one has.
In order to grant privileges to the user which is logged in (eg 'user1'), I had to sign out and log in using one of the superuser roles in that list (eg. 'user2'), using the following command:
psql -U 'user2' -h localhost 'database2'
where 'database2' is the name of the one that specific superuser 'user2' has privileges to.
Once you are logged in as a superuser, you can grant privileges to 'user1' by:
ALTER ROLE user1 WITH CREATEDB
or
ALTER ROLE user1 WITH SUPERUSER
Then sign in again as user1, who is now a superuser.
This blog was helpful as well as this link.
Currently, this worked for me:
sudo su postgres
psql
ALTER USER username WITH CREATEDB;
\q
exit
The root user is an account on the system independent from Postgres. There is only one root user.
A superuser is an account in Postgres with access to everything. There may be many superusers.
System accounts and Postgres accounts are different things, although unless you specify a Postgres username when you connect to the database (through utilities like psql, createdb, dropdb, or otherwise), it will use the current system user's name in hopes that there is a corresponding Postgres account with the same name. The root user does not, by default, have a corresponding account in Postgres.
When you install Postgres on *nix, it creates both a superuser named postgres and a system user named postgres.
Therefore, when you need to do something with Postgres as the built-in superuser, you have two options:
You may sudo su - postgres to become the postgres system user and execute your command (createdb, psql, etc). Because the system user has the same name as the database superuser, your command will connect as the appropriate account.
You may specify the username to execute as with the -U switch, eg psql -U postgres ....
Depending on your Postgres server's authentication settings, you may be required to enter a password with either or both connection methods.
What you can do when you have fresh installation of PostgreSQL is create your user with some rights (see createuser documentation):
my-user> sudo su - postgres -c "createuser <my-user> --createdb"
This will allow my-user to create DBs just like so:
my-user> createdb <my-db>
If you want the my-user to be able to do anything just use the --superuser flag instead:
my-user> sudo su - postgres -c "createuser <my-user> --superuser"
I got the same error and I found out that the reason was that I was trying to create a database outside of psql as a user which did not exist for postgresql. I found out about it and solved it by taking the following steps:
In my terminal I logged in as postgres user (the root user by default for postgresql) by typing sudo -u postgres psql
While inside the psql I typed \du to see all users and their privileges. I found out that I had only one user (the postgres one) and I had to create another superuser which had the same username as my Linux user (george)
I typed (still inside psql) CREATE USER george SUPERUSER; and this way I created a new super user called george.
I exited psql (by typing \q) and I was now able from outside psql, meaning from my terminal, to run created db <database name> with no issues at all.
Error ? You are trying to perform database actions( Creating Database, creating Roles) using a user that doesn't have the permission for those types of actions you are trying to perform.
solution ? Simply login to your database on the command line, i.e for PostgreSQL one will use "sudo -u postgres psql", then confirm that users specific assigned roles using the command "\du", most probably he/she doesn't have the necessary permissions to perform the actions you wanted. Then simply assign the roles you want the user to perform ,i.e create Database or simply make user "Superuser" by following along(https://chartio.com/resources/tutorials/how-to-change-a-user-to-superuser-in-postgresql/)

psql: permission denied for database "dbname" ("User does not have CONNECT privilege.") / "unrecognized role option 'connect'"

when I try to login to my database with psql, doing this:
psql dbname --username=qgis --password
>>(prompts for password, entered password)
psql: FATAL: permission denied for database "gisdatabase"
DETAIL: User does not have CONNECT privilege.
I've searched around on Google for information on this simple issue but haven't found anyone directly talking about this.
I've tried doing this:
psql dbname
>>ALTER ROLE qgis WITH CONNECT;
But got this error:
ERROR: unrecognized role option "connect"
So once again, here I am, asking yet another question on stackoverflow. Thanks for your time folks
You need to grant a privilege. Try this:
psql dbname
>> GRANT CONNECT ON DATABASE dbname TO qgis;
I assume you will also need further privileges. PostgreSQL has one of the best documentation pages of all the DBMSs: http://www.postgresql.org/docs/9.0/static/sql-grant.html (You can choose the postgres version you're using at the top of the page).