create database user with postgres on azure - postgresql

I need to create an additional user on postgres running on azure usually I would do :
create user myfineuser with password 'myfinepassword';
grant connect on database myfinedatabase to myfineuser;
But when I then try to connect to the db on azure with
psql "host=myfinehost.postgres.database.azure.com port=5432 \
dbname=myfinedatabase user=myfineuser password=myfinepassword sslmode=require"
I do receive
psql: FATAL: Invalid Username specified. Please check the Username and retry connection. The
Username should be in <username#hostname> format.
I did not fine how to specify user#host in the sql statement to create the user
myfineuser#myfinehost, 'myfineuser#myfinehost', .... all these permutations do result in a syntax error
How can i do this with psql. This article just talks about user but not how to specify user#host https://learn.microsoft.com/da-dk/azure/postgresql/howto-create-users#how-to-create-database-users-in-azure-database-for-postgresql

The solution ist simple :
create user myfineuser with password 'myfinepassword';
grant connect on database myfinedatabase to myfineuser;
then in psql :
psql "host=myfinehost.postgres.database.azure.com port=5432 \
dbname=myfinedatabase user=myfineuser#myfinehost password=myfinepassword sslmode=require"
note the myfineuser#myfinehost in the psql connect string. This is implicit

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.

How can I connect to a database as another user?

Im currently creating an API for a school project and everything is working good. My setup is: Node v10, Postgres, Koa and so on...
I currently have this:
CREATE ROLE sa WITH LOGIN PASSWORD 'some-password.';
CREATE DATABASE master WITH OWNER sa;
\c master;
When the init script runs in the docker machine the output I get is this one:
CREATE ROLE
CREATE DATABASE
You are now connected to database "master" as user "postgres".
So I did change the file to something like this:
CREATE ROLE sa WITH LOGIN PASSWORD 'some-password.';
CREATE DATABASE master WITH OWNER sa;
CONNECT TO master AS main USER sa;
And I get a syntax error:
STATEMENT: CONNECT TO master AS sa USER sa;
psql:/docker-entrypoint-initdb.d/init.sql:4: ERROR: syntax error at or near "CONNECT"
I can't find anywhere in docs (or haven't look very good) how to connect from a .sql file to a database with an specific user.
How would I connect to 'master' with its owner, which is 'sa' from a .sql file?
You can do this via this command in psql:
\c db_name usr_name
Here my assumption is you have a SQL file called "a.sql" and contains the lines of code in the file.
CREATE ROLE sa WITH LOGIN PASSWORD 'some-password.';
CREATE DATABASE master WITH OWNER sa;
\c master;
Now you are running this script using "psql" command line interface (CLI), so you get the message as below...
CREATE ROLE
CREATE DATABASE
You are now connected to database "master" as user "postgres".
Now, the thing to be noted that you have connected to database "master" because of "\c master" with user "postgres" because of you passed the credential of "postgres" user. Is I am right here then, If yes, then you have to pass the user and password for different user i.e. "sa" as below.
psql -h localhost -p 5432 -U sa -f a.sql master
Now, it will prompt for the password of "sa" user, finally, your script will run and you will be connected to "sa" user.
You can do this via this command in terminal
psql -d database -U username

Can't access superuser in postgresql

I can't seem to access my superuser account for my posgresQL database.
Using the command:
psql -U postgres
I sucessfully login into the user 'postgres'. However, this is not the default superuser. Doing:
=# \du
I get:
List of roles
Role name | Attributes | Member of
-----------+-------------------------------------+-----------
pgsql | Superuser, Create DB | {}
postgres | Create role, Create DB, Replication | {}
So 'pgsql' appears to be my default superuser.
When trying:
psql -U pgsql
I get the following error:
psql: FATAL: database "pgsql" does not exist
I changed the pg_hba.conf file to the following:
# Database administrative login by Unix domain socket
local all all trust
and also tried:
# Database administrative login by Unix domain socket
local all pgsql trust
but I still get the same error regardless, that database 'pgsql' does not exist.
Any help would be greatly appreciated, I need to be able to access the database's superuser.
just define database:
psql -U pgsql -d postgres
if you get error that postgres database does not exist, connect as postgres and list databases with \l

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

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