psql login fails database not found - postgresql

using postgresql 9.4 on ubuntu 15.04.
logged in as postrges
created following roles: fooadmin, fooread, fooupdate with appropriate privileges for the role.
created following user: fooadminuser
granted role to user: grant fooadmin to fooadminuser;
log out of psql
attempt to login to psql using the following:
psql -U fooadminuser -h localhost -W
get prompted for password/enter password
receive following error message:
psql: FATAL: database "fooadminuser" does not exist
I thought I was doing this correctly by creating the roles, then creating the login users, then login with the user that has the createdb role and create the database.
Is there a way to override during login the attempt to access a non-existent database?

You have to connect to an existing database. If You don't provide it, the default is the same as the user. After installation there should be one database named 'postgres'. Try this command:
psql -U fooadminuser -h localhost -d postgres -W

Related

psql: sudo: unknown user "abhishek"

I have just started with Postgresql and I know that on installation, a default user postgres is created. Now I have created another role/user abhishek with:
createuser --interactive
This role has superuser permission.
So I while being logged in as postgres added LOGIN role and defined the password for role abhishek using:
ALTER ROLE abhishek WITH LOGIN PASSWORD 'pg13'
But when I try to login with this role using:
sudo -i -u abhishek
It throws the following error:
sudo: unknown user: abhishek
sudo: unable to initialize policy plugin
So any hints as to where I'm going wrong be would be welcomed.
You created a Postgres user, not a Linux user. sudo runs a Linux command under a different Linux user account. If you want to log in to Postgres using your newly created database user, tell psql that:
psql -U abhishek <name of database to connect to>

createuser command for postgres failing on windows

I installed postgresql on Windows. When I run createuser in the DOS prompt, it fails with the following error:
createuser testuser
could not connect to database postgres : FTAL: role testuser does not exist
I have tried switching the pg_hba file from md5 to trust, but that has not solved the issue. Any thoughts? The database server itself is running- I was able to connect to it using another tool. Also, the path has a reference to the postgres/bin directory.
you need to specify a super user account in order to create a user
createuser -U pgsql testuser
if you plan on using a password for this user you can use -P or --pwprompt
createuser -P -U pgsql testuser
and it will prompt you for the password.
replace pgsql with a superuser account.

PostgreSQL error Fatal: role “username” does not exist

I'm setting up my PostgreSQL 9.1 in windows.
I can't do anything with PostgreSQL: can't createdb, can't createuser; all operations return the error message
Fatal: role root does not exist
root is my account name, which I created while installing Postgresql
But I am able to connect using:
username : postgres
How can I connect to postgres using role root?
There is a solution mentioned for linux platforms using su command here but not able to figure out solution for windows7
Thanks in Advance
If you want to login to Postgres using the username root you need to first create such a user.
You first need to login as the Postgres super user. This is typically postgres (and is specified during installation):
psql -U postgres <user-name>
Then you can create roles and databases:
psql (9.4.0)
Type "help" for help.
postgres=# create user root with password 'verysecret';
CREATE ROLE
postgres=# \q
c:\
c:\>psql -U root postgres
psql (9.4.0)
Type "help" for help.
postgres=>
Logged in as the superuser you can also grant the root user the necessary privileges.
All parameters for psql are documented in the manual.
Creating users and databases is also documented in the manual:
connecting to the database
create user
create database
In some cases, when you install postgres the initial DB is not created.
You need to execute initdb.
Same issue appeared while restoring DB/table on postgres docker container .
. When you connect to Postgres DB(psql shell) from inside the docker container, the default user would be a "root" (unless you specify psql -U "some-user-name")
[Manjunath-MacBook-Air:$ sudo docker exec -it a2ff6075344e bash
bash-5.0# psql -U postgres postgres < testdb_pg_dump
So, the issue gets resolved, by logging to psql shell with appropriate username
Here , -U postgres specifies that user connecting to DB is "postgres"

Setting up postgres on VPS for production

So, I have a Pyramid app with a postgres database on my local machine. I did a pg_dump to get a dump of the data in my database, called pg_dump_2014-04-22. I then git pushed this file, and did a git pull in the VPS to get the file.
Now, I have already installed postgres on my VPS. When I sudo -u postgres psql on my VPS, I can connect to it but there are no relations (naturally).
Both my username and database name are postgres.
So, I tried psql postgres < pg_dump_2014-04-22, but this gives the error psql: FATAL: role "root" does not exist.
I also tried pg_restore -h localhost -U postgres -d postgres pg_dump_2014-04-22, and that prompts me for my password, but then throws the error pg_restore: [archiver(db)] connection to database "postgres" falied: FATAL: password authentication failed for user postgres"
What am I missing here?
You first have to create a user and the database where you want to import your dump
su postgres
createuser root
createdb yourdb
Then import the dump with
psql -d yourdb -f pg_dump_2014-04-22

Postgres - How to use psql to create a database with the -c command and password authentication?

If I run the following:
psql -h localhost -U admin -c "CREATE DATABASE sales OWNER admin;"
It returns:
psql FATAL: database "admin" does not exist
I need to use password authentication and have setup .pgpass as follows:
localhost:*:*:admin:admin
Any ideas?
By default, when you connect as a user, the database connected to is the DB of the same name as the user.
If you want to connect to a different DB, you must specify it in the psql command line. Since in this case you're trying to create the DB, you can't connect to it yet, you must connect to another DB (like template1 or postgres) that already exists.
E.g.
psql -h localhost -U admin template1 -c "CREATE DATABASE sales OWNER admin;"
^^^^^^^^^