Обсуждение: [NOVICE] Why psql connection assumes default database name as the username

Поиск
Список
Период
Сортировка

[NOVICE] Why psql connection assumes default database name as the username

От
Neha Khatri
Дата:
Hi,

For psql connection, the default database name is assumed to be operating system user name. Is it a common practice to have a database name same as an OS username. Or would the applications create a database with the OS username as some default procedure?

Is this assumption made so that the owners of the database can be easily classified based on the database name. Or it is for some other reason.

Without the presence of database with same name as the username, following failure is seen. 
psql: FATAL: database "pgadmin" does not exist

In my specific case the problem is just that the database 'pgadmin' is not there, then why should it be a FATAL error. I understand that any process aborting errors are treated FATAL. But it looks like a simple error here.

It will be good to understand the reason behind default database name assumption.

Regards,
Neha

Re: [NOVICE] Why psql connection assumes default database name asthe username

От
hubert depesz lubaczewski
Дата:
On Thu, Mar 23, 2017 at 03:57:58PM +1100, Neha Khatri wrote:
> For psql connection, the default database name is assumed to be operating
> system user name. Is it a common practice to have a database name same as
> an OS username. Or would the applications create a database with the OS
> username as some default procedure?

it's not that database name is assume to be operating system user name.

psql (or specifically libpq) makes two assumptions:
1. if username is not given - use operating system user name
2. if database name is not given - use username as db name.

So, if you don't have anything set in environment, and you're logged in
a neha, then:

$ psql
will try to connect to db neha with user neha

$ psql -U depesz
will try to connect to db depesz with user depesz

$ psql -d xxx
will try to connect to db xxx with user nega

$ psql -U depesz -d xxx
will try to connect to db xxx with user depesz

> In my specific case the problem is just that the database 'pgadmin' is not
> there, then why should it be a FATAL error. I understand that any process
> aborting errors are treated FATAL. But it looks like a simple error here.

Levels or errors are clearly defined:
1. ERROR means that something was wrong, but you can still continue to
   work in the db connection
2. FATAL means that something wrong happened, and you can't use the
   connection any more as it has closed.

Since failing to open db connection prohibits you from doing any more
work in the db connection (which is not there) - it's clearly FATAL and
not ERROR.

depesz


Re: [NOVICE] Why psql connection assumes default database name as the username

От
Neha Khatri
Дата:
> For psql connection, the default database name is assumed to be operating
> system user name. Is it a common practice to have a database name same as
> an OS username. Or would the applications create a database with the OS
> username as some default procedure?

psql (or specifically libpq) makes two assumptions:
1. if username is not given - use operating system user name
2. if database name is not given - use username as db name.

So, if you don't have anything set in environment, and you're logged in
a neha, then:

$ psql
will try to connect to db neha with user neha

$ psql -U depesz
will try to connect to db depesz with user depesz

I had the question specific to above two cases, when the database name is not provided. There is no surety of having a database name same as the user name. Then why assume the database name and try to connect to it. Is it due to legacy reason or due to some common database naming practice?

> In my specific case the problem is just that the database 'pgadmin' is not
> there, then why should it be a FATAL error. I understand that any process
> aborting errors are treated FATAL. But it looks like a simple error here.

Levels or errors are clearly defined:
1. ERROR means that something was wrong, but you can still continue to
   work in the db connection
2. FATAL means that something wrong happened, and you can't use the
   connection any more as it has closed.

Since failing to open db connection prohibits you from doing any more
work in the db connection (which is not there) - it's clearly FATAL and
not ERROR.

In this case the database does not exist, then connecting to a non existent database does not seem a FATAL issue. May if connection to an existent database fails, it definitely would be a FATAL issue. Though I understand that this thought is not in line with the protocol defined for error severity. Still does not appear to be a FATAL issue.

Regards,
Neha


Re: [NOVICE] Why psql connection assumes default database name as the username

От
"David G. Johnston"
Дата:
On Thu, Mar 23, 2017 at 2:49 PM, Neha Khatri <nehakhatri5@gmail.com> wrote:

I had the question specific to above two cases, when the database name is not provided. There is no surety of having a database name same as the user name. Then why assume the database name and try to connect to it. Is it due to legacy reason or due to some common database naming practice?

I have never named a database after a person/user ...​ myself or otherwise.

I suspect in the early days the user running psql was typically "postgres".  Since the database "postgres" is created by default if the postgres O/S user runs psql without any arguments it will connect to existing "postgres" database.


> In my specific case the problem is just that the database 'pgadmin' is not
> there, then why should it be a FATAL error. I understand that any process
> aborting errors are treated FATAL. But it looks like a simple error here.

Levels or errors are clearly defined:
1. ERROR means that something was wrong, but you can still continue to
   work in the db connection
2. FATAL means that something wrong happened, and you can't use the
   connection any more as it has closed.

Since failing to open db connection prohibits you from doing any more
work in the db connection (which is not there) - it's clearly FATAL and
not ERROR.

In this case the database does not exist, then connecting to a non existent database does not seem a FATAL issue.

If psql had some concept of "working but disconnected" I might agree with this sentiment but since its either connected or not running​ not finding the database its looking for means it has "stop running" which means it is dead (i.e., FATAL)

David J.

Re: [NOVICE] Why psql connection assumes default database name as the username

От
Neha Khatri
Дата:
On Fri, Mar 24, 2017 at 9:16 AM, David G. Johnston <david.g.johnston@gmail.com> wrote:
On Thu, Mar 23, 2017 at 2:49 PM, Neha Khatri <nehakhatri5@gmail.com> wrote:

I had the question specific to above two cases, when the database name is not provided. There is no surety of having a database name same as the user name. Then why assume the database name and try to connect to it. Is it due to legacy reason or due to some common database naming practice?

I have never named a database after a person/user ...​ myself or otherwise.

I suspect in the early days the user running psql was typically "postgres".  Since the database "postgres" is created by default if the postgres O/S user runs psql without any arguments it will connect to existing "postgres" database.

I get that. So, in current database systems, how much does this assumption weigh? Or it would be more useful update the message from

psql: FATAL: database "neha" does not exist    //Ofcourse, it won't in most cases.

to

psql: FATAL: database "neha" does not exist. Try connecting to an existing database or template database.
 

In this case the database does not exist, then connecting to a non existent database does not seem a FATAL issue.

If psql had some concept of "working but disconnected" I might agree with this sentiment but since its either connected or not running​ not finding the database its looking for means it has "stop running" which means it is dead (i.e., FATAL)


Hmm, alright.

Neha

Re: [NOVICE] Why psql connection assumes default database name as the username

От
"David G. Johnston"
Дата:
On Thu, Mar 23, 2017 at 6:10 PM, Neha Khatri <nehakhatri5@gmail.com> wrote:

I get that. So, in current database systems, how much does this assumption weigh? Or it would be more useful update the message from

psql: FATAL: database "neha" does not exist    //Ofcourse, it won't in most cases.

to

psql: FATAL: database "neha" does not exist. Try connecting to an existing database or template database.


Introducing "template database" is probably going to cause more confusion, not less.

<database "neha" does not exist, please provide the name of one that does exist> - the last part seems somewhat self-evident ...

What you really want to say is:

FATAL: blah-blah-blah
HINT: While the database name argument is technically optional you probably want to supply one seeing as how a database with your name, i.e., the default, doesn't exist.

David J.

Re: [NOVICE] Why psql connection assumes default database name asthe username

От
hubert depesz lubaczewski
Дата:
On Fri, Mar 24, 2017 at 08:49:11AM +1100, Neha Khatri wrote:
> I had the question specific to above two cases, when the database name is
> not provided. There is no surety of having a database name same as the user
> name. Then why assume the database name and try to connect to it. Is it due
> to legacy reason or due to some common database naming practice?

so what would you prefer to do, as psql, in such case? report error
unconditionally? after all - if user didn't provide db name, and you
don't want to assume db name that is the same as user name - then you
don't know what db to connect to.

Best regards,

depesz



Re: [NOVICE] Why psql connection assumes default database name as the username

От
Neha Khatri
Дата:

On Fri, 24 Mar 2017 at 5:25 pm, hubert depesz lubaczewski <depesz@depesz.com> wrote:
On Fri, Mar 24, 2017 at 08:49:11AM +1100, Neha Khatri wrote:
> I had the question specific to above two cases, when the database name is
> not provided. There is no surety of having a database name same as the user
> name. Then why assume the database name and try to connect to it. Is it due
> to legacy reason or due to some common database naming practice?

so what would you prefer to do, as psql, in such case? report error
unconditionally? after all - if user didn't provide db name, and you
don't want to assume db name that is the same as user name - then you
don't know what db to connect 

Yes, so psql should say "Not sure which database to connect to. Please specify a valid database name using -d option".
For backwards compatibility, psql can still attempt to connect to db with name same as the username, but the error message could be more like providing the information, what can be done next.

Regards,
Neha

--
Cheers,
Neha

Re: [NOVICE] Why psql connection assumes default database name asthe username

От
hubert depesz lubaczewski
Дата:
On Fri, Mar 24, 2017 at 08:39:30AM +0000, Neha Khatri wrote:
> Yes, so psql should say "Not sure which database to connect to. Please
> specify a valid database name using -d option".
> For backwards compatibility, psql can still attempt to connect to db with
> name same as the username, but the error message could be more like
> providing the information, what can be done next.

Sorry, but I don't agree.
I use databases named like users all the time, and love the fact that
I don't have to provide dbname when it's obvious.

Error message is, in my opinion, clear enough - you're trying to connect
to database that doesn't exist. Why you are trying to - impossible to
tell. Maybe you made a typo. Maybe you forgot to add option. Maybe you
forgot to export env variable.

What is being chosen as user/database name is documented in psql docs.
Additionally - if you want to require that user provides database name,
why don't require also host and port? After all, you need all
four parameters to connect: host, port, username and database name.

If all you ever want is to connect to db named "x", then why not using
psql -d x, or even setting PGDATABASE to x?

depesz



Re: Why psql connection assumes default database name as the username

От
Neha Khatri
Дата:
On Fri, Mar 24, 2017 at 7:57 PM, hubert depesz lubaczewski <depesz@depesz.com> wrote:
On Fri, Mar 24, 2017 at 08:39:30AM +0000, Neha Khatri wrote:
> Yes, so psql should say "Not sure which database to connect to. Please
> specify a valid database name using -d option".
> For backwards compatibility, psql can still attempt to connect to db with
> name same as the username, but the error message could be more like
> providing the information, what can be done next.

Sorry, but I don't agree.
I use databases named like users all the time, and love the fact that
I don't have to provide dbname when it's obvious.

So, is it common practice to have a database name same as the username. What are the usecase advantages of the database name and username mapping? It would be good to understand.

Regards,
Neha

Re: Why psql connection assumes default database name asthe username

От
hubert depesz lubaczewski
Дата:
On Mon, Mar 27, 2017 at 09:37:44AM +1100, Neha Khatri wrote:
> > Sorry, but I don't agree.
> > I use databases named like users all the time, and love the fact that
> > I don't have to provide dbname when it's obvious.
> So, is it common practice to have a database name same as the username.
> What are the usecase advantages of the database name and username mapping?
> It would be good to understand.

Why wouldn't I use the name of user?

I can have multiple users on a servers that want to "play" with
database, so using db name the same as user name makes it trivial to use
and figure out who is using which db.

Best regards,

depesz



Re: Why psql connection assumes default database name as the username

От
jesusthefrog
Дата:
This also (intentionally or not) matches Oracle's behavior. That is: by default the user connecting is effectively the same as the database you are connecting to.

--
-----BEGIN GEEK CODE BLOCK-----
  Version: 3.12
  GIT d- s+ a- C++++ L+++ S++ B+ P++>++++ E++ W+++
  N o? K- !w++++ O- M- V? PS++ PE- Y+ PGP t+ 5+++
  X R+ tv b+++ DI++ D++ G+ e-- h- r++ y
------END GEEK CODE BLOCK------

Re: Why psql connection assumes default database name as the username

От
Neha Khatri
Дата:
On Tue, Mar 28, 2017 at 6:00 AM, jesusthefrog <jesusthefrog@gmail.com> wrote:
This also (intentionally or not) matches Oracle's behavior. That is: by default the user connecting is effectively the same as the database you are connecting to.

Yes, some research on internet indicates that in Oracle the User and the Schema have the same name, not the same database name though. Also, the User/Schema name seem to be a database User name not an operating system username. The sample User/Schema names were 'oe'(order entry), 'hr' (human resources), etc. Somewhere I also read  "User is an account to connect to a database and a schema is a set of objects (table, view, etc) that belong to that account".

Can a similar definition be applied in PostgreSQL for User and Database name mapping, i.e  if a database name is same as a user name then the access privileges for that datbase are guided by the properties defined for that user in view pg_roles?

Further investigating, here is snippet from PostgreSQL documentation:

"PostgreSQL manages database access permissions using the concept of roles.
Database roles are conceptually completely separate from operating system users. In practice it might be convenient to maintain a correspondence, but this is not required.
Every connection to the database server is made using the name of some particular role, and this role determines the initial access privileges for commands issued in that connection. The role name to use for a particular database connection is indicated by the client that is initiating the connection request in an application-specific fashion.  For example, the psql program uses the -U command line option to indicate the role to connect as. Many applications assume the name of the current operating system user by default (including createuser and psql). Therefore it is often convenient to maintain a naming correspondence between roles and operating system users."

So the OS username and database name mapping seem to be in place for convenience as depesz suggested (and probably as an aid to determine the access privileges for a database by seeing the properties of the role that has the same name as the database, if present).

Regards,
Neha

Re: Why psql connection assumes default database name as the username

От
"David G. Johnston"
Дата:
On Mon, Mar 27, 2017 at 5:58 PM, Neha Khatri <nehakhatri5@gmail.com> wrote:
Can a similar definition be applied in PostgreSQL for User and Database name mapping, i.e  if a database name is same as a user name then the access privileges for that datbase are guided by the properties defined for that user in view pg_roles?

​Nowhere in "pg_roles" does the name of a specific database appear - so no.

You defines roles.  You define databases.  You give roles CONNECT permissions on databases if you want them to be allowed to connect to said databases.  Of course, superusers can connect to anything.  That is, along as pg_hba.conf allows them to attempt a connection in the first place.

What I said above is wrong, though, if one leaves the default permissions of the implicit PUBLIC group in place.  By default all users are allowed to CONNECT to any database in the cluster - among other permissions.

So while creating a database of the same name of as a user "just works" it is working because of a default that can be changed.  The fact that the names match doesn't enter into it.


David J.


Re: Why psql connection assumes default database name as the username

От
Neha Khatri
Дата:
On Tue, Mar 28, 2017 at 12:17 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:
On Mon, Mar 27, 2017 at 5:58 PM, Neha Khatri 
Can a similar definition be applied in PostgreSQL for User and Database name mapping, i.e  if a database name is same as a user name then the access privileges for that database are guided by the properties defined for that user in view pg_roles?

​Nowhere in "pg_roles" does the name of a specific database appear - so no.

So while creating a database of the same name of as a user "just works" it is working because of a default that can be changed.  The fact that the names match doesn't enter into it.

Right, the database does not appear in pg_roles. So having a database name same as a user in pg_roles, does not have anything to do with guiding the access privilege for the database.

It seems that for convenience, the OS username and the DB name correspondence could be in place. 
Many other PostgreSQL utilities like createdb, clusterdb, vacuumdb, reindexdb, pg_dump assume the dbname same as username, in case it is neither explicitly specified as an option nor it is set in the environment variable PGDATABASE.


Regards,
Neha