Problems with createdb in postgres

我怕爱的太早我们不能终老 提交于 2019-12-08 05:37:50

问题


I have to run a simulation with several postgresql databases spread on different machines which all of them are running linux.

I successfully compiled and built postgresql from the source code and I can also run the server, but when I try to create a new db with this command:

./postgresql/bin/createdb db1

I get this error:

createdb: could not connect to database postgres: FATAL:  role "giulio" does not exist

where giulio is my username to access all the machines.

On some machine it works while on other it does not. I really cannot figure out the root of the problem. I suppose it is something related with the access control of postgres.

I did several research on google but I was not able to found and to solve the problem.

Does anyone know how to get this work?

Thanks,

-Giulio


回答1:


I would assume that on the machines where the user "giulio" is already known, you executed initdb with exactly this user making him the DB superuser. A quote from inidb(1) (emphasis mine):

--username=username Selects the user name of the database superuser. This defaults to the name of the effective user running initdb. It is really not important what the superuser's name is, but one might choose to keep the customary name postgres, even if the operating sys‐ tem user's name is different.

On the other machines I assume you did execute initdb with another user, hopefully using postgres.

In order to get back on the standard track I propose, that you delete the database cluster on the machines where "giulio" is the superuser and setup a new database cluster using the standard postgres user. Then add another user "giulio". This will avoid more confusion down the road as some scripts/programs expect a superuser account named postgres.




回答2:


PostgreSQL has its own users and roles that are separate from that of your OS. Generally there is a dedicated super user, postgres. For user management info, look here:

http://www.postgresql.org/docs/9.1/interactive/user-manag.html

When executing postgres commands, you need to specify the user with the -U flag (unless you are already logged in as an existing db user). When you called the createdb script, because you didn't use the -U flag, the server assumed that the uid of the caller (giulo) should be used, but you didn't add a user "giulio" to the db, and hence the error message.

So execute the command as

./postgresql/bin/createdb -U postgres db1

and it should work. Then, later on, you may want to create other users and roles in your db rather than doing everything as the superuser.




回答3:


My answer was more simple...I realized I needed to just run the following:

brew install postgresql

then to check if it worked I ran:

which createdb 

to check the file path and if it worked, and it had ! Hope this helps.



来源:https://stackoverflow.com/questions/7988517/problems-with-createdb-in-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!