default password of postgres - postgresql

I am launching postgres conatainer by placing sql script in docker-initdb. Everything is running fine. But can someone tell what's the password of my database created with below script?
CREATE DATABASE mydb;
CREATE USER mydbadmin WITH ENCRYPTED PASSWORD 'mypwd';
ALTER DATABASE mydb owner to mydbadmin;
GRANT ALL PRIVILEGES ON DATABASE mydb TO mydbadmin;
GRANT CONNECT ON DATABASE mydb to mydbadmin;
I am not providing any explicit password for DB. What would be default password in this case?
How can I provide my explicit password?

A database cannot have a password.
Only roles (users) can authenticate with the PostgreSQL server, so only they can have a password.

Database has no password, users have passwords. There is no "default password" (i guess that means new users would have pre-set password)
User mydbadmin has the password you have set. Other users will have the password you set when you CREATE USER
Make sure you have necessary settings in pg_hba.conf

Related

postgres login asks for non superuser password

I set up postgres according to the instructions for Windows 10 but every time I try to run psql it asks for a non superuser password which I haven't created. How do I make it ask for the superuser without using psql -U postgres command every time? Or how can I set/change a password for a non superuser? I've tried using ALTER ROLE to change the password but get role [username] does not exist as an error message.
By default, psql tries to use your OS username as a database username. Presumably this role hasn't been created in your database, hence the "does not exist" error.
You can override this default by setting the PGUSER environment variable.

FATAL: password authentication failed for user postgres aurora

If I create a user logged in as postgres to the root db and create a user... it doesn't work. What am I doing wrong?
postgres=> CREATE ROLE myUser WITH LOGIN PASSWORD 'xxx';
CREATE ROLE
postgres=> GRANT CONNECT ON DATABASE myDatabase TO myUser;
GRANT
postgres=> GRANT USAGE ON SCHEMA public to myUser;
GRANT
postgres=> GRANT SELECT ON ALL TABLES IN SCHEMA public TO myUser;
GRANT
When I go to authenticate I get an error.
psql -h $dbURL -U myUser myDatabase
FATAL: password authentication failed for user myUser
The user you created is "myuser", because case is ignored for SQL identifiers not within double quotes, and folded to lower case. But case is not ignored in command-line tools, so you are trying to log in as non-existent user "myUser". Since non-existent users don't have a password hash, password authentication must fail.
You might want to check if you have a .pgpass file present and inspect the environment variables to see if you have set a PGPASSWORD or PGPASSFILE. In any of these cases, psql will not prompt for the password and take it from the file or variable instead - and if what's there is incorrect, it will give you that exact error.
You can
unset the PGPASSWORD or PGPASSFILE variable
get rid of the .pgpass file
remove the entry corresponding to this connection from .pgpass file
correct the value of PGPASSFILE/PGPASSWORD variable or the contents of .pgpass file
use psql with a -W switch making it ignore other settings and always prompt for a password
alter the host-based authentication settings in pg_hba.conf file to change the authentication method or trust a certain type of connection

postgres uses a database password or a user password

I imported a postgres database in my local postgres server.
I had to connect to the database (to allows django to retrive data) using the file called setup.local.
There is required to specify: DB_HOST=localhost, DB_NAME, DB_USER, DB_PASSWORD.
DB_HOST is localhost without any doubt. The DB_name is the one I choose importing (psql imported_db < downloaded_DB)
DB_USER is my_name (or I can change the owner ALTER DATABASE imported_db OWNER TO other_name).
The wire thing, for me, is that I have to use the user (either the my_name or other_name) password and not the database password (even if the variable name is DB_PASSWORD).
So the question:
does a psql database have a password or just the roles/users have ones and use them to access the database?
Andrea
Passwords are set for USER and ROLE only. A user may access multiple databases, according to the GRANTs for the ROLE.
See also:
https://www.postgresql.org/docs/10/static/ddl-priv.html
https://www.postgresql.org/docs/10/static/client-authentication.html
https://www.postgresql.org/docs/10/static/user-manag.html
DB_HOST=localhost is a key here. Look into the pg_hba.conf you will find ident against localhost connections most probably.
https://www.postgresql.org/docs/current/static/auth-methods.html#AUTH-IDENT
When ident is specified for a local (non-TCP/IP) connection, peer
authentication (see Section 20.3.6) will be used instead.
https://www.postgresql.org/docs/current/static/auth-methods.html#AUTH-PEER
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.

how to create db2 schema with password by command

I am creating db2 schema by using create schema schemaName in mydb database, now while I connect the database by writing connect to mydb user schemaName;
it prompts to give a password.
My question is how to set a password to my existing schema in db2 database
A schema cannot have a password. A user can have a password, but the user does not necessarily match any schema. In DB2 authentication is delegated to the operating system (or other external authority), so you need to use whatever password belongs to the operating system user "schemaName".

PostgreSQL public access to database

I recently created a test database but I would like it to be accessible to all users.
I created the database as user postgres then use the command
GRANT ALL ON DATABASE dbname TO public;
But when I log out and try to connect to it with a different user I get this error
psql -d dbname
psql: FATAL: role "username" does not exist
I don't want to have to create a user for each person who will be accessing the database which is the reason i wanted to make it public. Is there a permission that I am still missing?
Although access to everybody (public) is granted, the user you connect with has to exist. The default for psql is the username of your system account. You can, of course, create a passwordless anonymous user everybody can use to connect:
psql -U anonymous dbname