Set password in PostgreSQL - psql

I am trying to set new password in postgreSQL.But I am getting error like below.
[root#localhost ~]# sudo -u postgres psql
could not change directory to "/root"
Welcome to psql 8.1.23, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit`postgres=# \password postgres
Query buffer is empty.
\p: extra argument "assword" ignored
\p: extra argument "postgres" ignored`

This works for me.
ALTER USER postgres WITH ENCRYPTED PASSWORD 'password';

Try this? Link
Login to your database and use the ALTER USER Postgres WITH PASSWORD '<newpassword>'; command.

Highly recommended to avoid ever using the ALTER USER Postgres WITH PASSWORD... syntax as there is a possibility whatever password you submit will be saved forever in your on disk logs.
Whenever possible, the best way to do it is through an interactive psql CL like so:
psql=# \password user_name
If you cannot do that, consider using SCRAM-SHA-256 authentication and the method described here.

Related

Postgres server error on mac ["root" execution of the PostgreSQL server is not permitted. The server must be started under an unprivileged user ID]

Anyone to help me solve this issue that has been on my case for the last couple of hours? I have so far tried all suggested solutions but can't seem to find anything working.
Error:
"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise. See the documentation for
more information on how to properly start the server.
I will forever be grateful to anyone who helps me solve this.
Try, change you user in postgres
Switching Over to the postgres Account
Switch over to the postgres account on your server by typing:
sudo -i -u postgres
You can now access a Postgres prompt immediately by typing:
psql
This will log you into the PostgreSQL prompt, and from here you are free to interact with the database management system right away.
Exit out of the PostgreSQL prompt by typing:
\q
This will bring you back to the postgres Linux command prompt.
Accessing a Postgres Prompt Without Switching Accounts
You can also run the command you’d like with the postgres account directly with sudo.
For instance, in the last example, you were instructed to get to the Postgres prompt by first switching to the postgres user and then running psql to open the Postgres prompt. You could do this in one step by running the single command psql as the postgres user with sudo, like this:
sudo -u postgres psql
This will log you directly into Postgres without the intermediary bash shell in between.
Again, you can exit the interactive Postgres session by typing:
\q
Many use cases require more than one Postgres role. Read on to learn how to configure these.
Steps to properly install Postgres via Homebrew on Mac M1:
brew install postgres [Maybe you need to use the alternative install method, as described here: https://docs.brew.sh/Installation#alternative-installs]
initdb /Users/<username>/db -E utf8
[This initializes postgres to use the given directory as the database directory. Normally it is not adviced to use the user directory for database storage.]
pg_ctl -D /Users/<username>/db -l logfile start
[After getting success with step 2 it will prompt to run step 3. This command manually starts the server.]

Password authentication failed for a user not in my role list

I am currently trying to run a service using ruby on rails, while running the command rake db:schema:load
I got a password not provided error, checking my database.yml file I found this line:
#password:
I removed the comment but got the same error so I actually specified a dummy password like so:
password: dummy
Then I got this error
password authentication failed for user "ivan"
After this I tried this solution to actually set my password as dummy I still get the same error, my pg_hba.conf has the following line:
local all all peer
Changing peer to md5 produces the same error, after checking my postgres roles the only one listed is "postgres" not "ivan", what is the cause of this role appearing when trying to run my command and how do I fix it?
You need to create new user in postgres . please try below commands for creating new role and user
sudo su postgres
psql
du # for checking all existing roles
CREATE ROLE ivan WITH LOGIN SUPERUSER PASSWORD 'ivan123';
du # check whether this role created or not
then add database user name as 'ivan' and password as 'ivan123' in your database.yml

creating user for postgresql on rails

I chose postgresql for my database for rails but i ran into an apparently common error where 'FATAL: role "app" does not exist' when i try to run rake db:create:all. I found two solutions but im not sure which one is the right one to use. One website says to
su -
su - postgres
createuser -s Application
exit
exit
while the other says to
su - postgres
create role myapp with createdb login password 'password1'
what's the difference between the two? and which one should i use?
You should use this for the development environment only
Login in postgres console:
$> sudo -u postgres psql
create user with name rails and password:
=# create user rails with password 'password';
make user rails superuser:
=# alter role rails superuser createrole createdb replication;
create database projectname with owner rails:
=# create database projectname owner rails;
in database.yml:
development:
adapter: postgresql
encoding: unicode
database: projectname
pool:
username: rails
password: password
To create a user type this in your console
createuser
Enter the username and make it a superuser
Also you could add a password also by adding pwprompt to createuser like this
createuser --pwprompt
Next you enter pgsql as postgres and create your database
sudo -u postgres psql postgres
create database db_name owner user_name;
Hope it helps
Refer this site for more details
https://www.digitalocean.com/community/articles/how-to-install-and-use-postgresql-on-ubuntu-12-04
The second option is invalid. That does not work.
The first one:
I don't recommend the first one and depending on your operating system some of the steps are unnecessary.
You become a superuser and make the shell a login shell (as if you would log in directly). I think it is risky and not necessary. Why not suing directly to postgre user (that's just a matter of setup)?
You become postgre and make the shell a login shell
You create a superuser Application in PostgreSQL. So you have the privileges of a superuser for your application. I think that is risky too. Superuser for an application is very bad. Often you need createdb, login and password to get started and other privileges, if ever needed, can be added/revoked later.
You quit the postgres shell session.
You quit the superuser shell session
The second one:
That's not correct. You forget to execute psql command as second step. The second command would become the third. It would be like that.
You become postgres and make the shell a login shell
You log into postgres command shell by default with user postgres
You create a role myapp with options createdb, login and (usually by default encrypted) password with value password1.
The difference is that the first one grants superuser privileges to the Application role while the second grants only the listed permissions to the myapp role.
The second option is more secure. Among the two option opt for the seoconde one.
Note: In PostgreSQL a user is simply a role with login permission. It's more of a conceptual distinction. Technically a user and role are pretty much the same as far as I know.

Rails and PostgreSQL: Role postgres does not exist

I have installed PostgreSQL on my Mac OS Lion, and am working on a rails app. I use RVM to keep everything separate from my other Rails apps.
For some reason when I try to migrate the db for the first time rake cannot find the postgres user. I get the error
FATAL: role "postgres" does not exist
I have pgAdmin so I can clearly see there is a postgres user in the DB - the admin account in fact - so I'm not sure what else to do.
I read somewhere about people having issues with PostgreSQL because of which path it was installed in, but then I don't think I would have gotten that far if it couldn't find the db.
Actually, for some unknown reason, I found the issue was actually because the postgresql role hadn't been created.
Try running:
createuser -s -r postgres
Note that roles are the way that PostgreSQL maintains database permissions. If there is no role for the postgres user, then it can't access anything. The createuser command is a thin wrapper around the commands CREATE USER, CREATE ROLE, etc.
Recently i got this problem immediately after installing postgres.
If it comes immediately after installation, you might be missing the default user, postgres.
In that case, you can create default user postgres using below command.
createuser -s -U $USER
Ex: createuser -s -U $USER
enter your required role name: postgres
enter password for your the user:
It will prompt you to enter required database role name and password
Once you complete the process, you can login to the postgres console using below command
psql -U 'your_database_name'
Ex: psql -U postgres
Here, You need to enter the password if you have given any, while creating the user.
Hope it helps :)
I was on OSX 10.8, and everything I tried would give me the FATAL: role "USER" does not exist. Like many people said here, run createuser -s USER, but that gave me the same error. This finally worked for me:
$ sudo su
# su postgres
# createuser -s --username=postgres MYUSERNAME
The createuser -s --username=postgres creates a superuser (-s flag) by connecting as postgres (--username=postgres flag).
I see that your question has been answered, but I want to add this answer in for people using OSX trying to install PostgreSQL 9.2.4.
This message pops up, when the database user does not exist. Compare the manual here.
Multiple local databases cannot be the explanation. Roles are valid cluster-wide. The manual again:
Note that roles are defined at the database cluster level, and so are
valid in all databases in the cluster.
You must be ending up in another database-cluster. That would be another server running on the same machine, listening to a different port. Or, more likely, on a different machine.
Could it be that the message comes, in fact, from the remote server?
I met this issue right on when I first install the Heroku's POSTGRES.app thing. After one morning trial and error i think this one line of code solved problem. As describe earlier, this is because postgresql does not have default role the first time it is set up. And we need to set that.
sovanlandy=# CREATE ROLE postgres LOGIN;
You must log in to your respective psql console to use this psql command.
Also noted that, if you already created the role 'postgre' but still get permission errors, you need to alter with command:
sovanlandy=# ALTER ROLE postgres LOGIN;
Hope it helps!
In the Heroku documentation; Getting started whit rails 4, they say:
You will also need to remove the username field in your database.yml
if there is one so: In file config/database.yml remove: username:
myapp
Then you just delete that line in "development:", if you don't pg tells to the database that works under role "myapp"
This line tells rails that the database myapp_development should be
run under a role of myapp. Since you likely don’t have this role in
your database we will remove it. With the line remove Rails will try
to access the database as user who is currently logged into the
computer.
Also remember to create the database for development:
$createdb myapp_development
Repleace "myapp" for your app name
The installation procedure creates a user account called postgres that is associated with the default Postgres role. In order to use Postgres, you can log into that account. But if not explicitly specified the rails app looks for a different role, more particularly the role having your unix username which might not be created in the postgres roles.
To overcome that, you can create a new role, first by switching over to the default role postgres which was created during installation
sudo -i -u postgres
After you are logged in to the postgres account, you can create a new user by the command:
createuser --interactive
This will prompt you with some choices and, based on your responses, execute the correct Postgres commands to create a user.
Pass over a role name and some permissions and the role is created, you can then migrate your db
Could you have multiple local databases? Check your database.yml and make sure you are hitting the pg db that you want. Use rails console to confirm.
My answer was much more simple. Just went to the db folder and deleted the id column, which I had tried to forcefully create, but which is actually created automagically. I also deleted the USERNAME in the database.yml file (under the config folder).
In Ubuntu local user command prompt, but not root user, type
sudo -u postgres createuser username
username above should match the name indicated in the message "FATAL: role 'username' does not exist."
Enter password for username.
Then re-enter the command that generated role does not exist message.
I ended up here after attempting to follow Ryan Bate's tutorial on deploying to AWS EC2 with rubber. Here is what happened for me:
We created a new app using "
rails new blog -d postgresql
Obviosuly this creates a new app with pg as the database, but the database was not made yet. With sqlite, you just run rake db:migrate, however with pg you need to create the pg database first. Ryan did not do this step. The command is rake db:create:all, then we can run rake db:migrate
The second part is changing the database.yml file. The default for the username when the file is generated is 'appname'. However, chances are your role for postgresql admin is something different (at least it was for me). I changed it to my name (see above advice about creating a role name) and I was good to go.
Hope this helps.
After a bunch of installing and uninstalling of Postgres, here's what now seems to work consistently for me with Os X Mavericks, Rails 4 and Ruby 2.
In the database.yml file, I change the default usernames to my computer's username which for me is just "admin".
In the command line I run rake db:create:all
Then I run rake db:migrate
When I run the rails server and check the local host it says "Welcome aboard".
You might be able to workaround this by running initdb -U postgres -D /path/to/data or running it as user postgres, since it defaults to the current user. GL!

rake db:create fails, authentication problem with postgresql 8.4

first things first, please excuse my utter noobness. I really tried to find a solution out there, but now i'm stuck and completely clueless.
i'm trying to deploy a rails 3 app on a distant server ; when developping on my local VM, no problem showed. But now, when i try to run
rake db:create
it fails, with error (here translated, since i'm french):
FATAL : password authentication failed for user <<mylogin>>
here's my database.yml :
login: &login
adapter: postgresql
username: mylogin
password: mypassword
host: localhost
port: 5432
encoding: UTF8
development:
<<: *login
database: somesite_development
test:
<<: *login
database: somesite_test
production:
<<: *login
database: somesite_production
the user "mylogin" has been created postgre-side with the command-line tool "createuser". It's authorized to create dbs.
postgresql.conf configures the server to listen on localhost.
I've tried many things with pg_hba.conf, none worked - whatever the method used (ident, password, md5) for user "mylogin" on 127.0.0.1, authentication fails - though i've never had problems connecting / creating dbs with psql.
any clue ?
EDIT: okay, found out how incredibly stupid i've been... the password for my user was simply not set !
I think i forgot the semicolon after
ALTER USER xxxx WITH PASSWORD xxxx ;
... i saw this by requesting "SELECT * FROM pg_shadow;" - the password field was empty. Three days of my life wasted because of this dumb mistake...
I was also stuck on this problem for a really long time, and went to a variety of links (including the ones offered in this post) to try and find the answer, but to no avail. However, the solution is very simple. While many of the other responses were on the right track here are the exact steps to solve the problem:
Open your pg_hba.conf file in a text editor of your choice. (It is located in /etc/postgresql//main)
Navigate to the line that reads:
# "local" is for Unix domain socket connections only
and paste below it:
local all all trust
This will trust all unix users trying to connect to the psql server on the local machine. (Read documentation at top of page for further info about function of columns)
Save the pg_hba.conf file and exit the text editor.
Restart the Postgresql server by running the command:
service postgresql restart
Now try and start a psql server by running:
psql -d -U (or "psql " for short)
You should be able log in with no problem.
*Note:
All this assumes that you have have a valid psql username for logging in. If you don't follow the links below to set one up:
Setting up a user:
http://erikonrails.snowedin.net/?p=274
Making sure you have a valid postgres user:
http://archives.postgresql.org/pgsql-novice/2002-08/msg00072.php
Listing all existing psql users:
If you are looking for a "LIST USERS" or "DISPLAY USERS" command then try:
"select * from pg_user;" (when logged in to psql)
Good luck!
okay, found out how incredibly stupid i've been... the password for my user was simply not set ! I think i forgot the semicolon after
ALTER USER xxxx WITH PASSWORD xxxx ;
... i saw this by requesting "SELECT * FROM pg_shadow;" - the password field was empty. Three days of my life wasted because of this dumb mistake...
I had same problem. In my case it was because in my database.yml file the username started with capital letter but in database it was all lower case.
Solution: When creating user from postgres command line, it converts all letters to lowercase. For using capital letters, double quotes must be used.
Example:
create user AlbertEinstein; result = alberteinstein
vs
create user "AlbertEinstein"; result = AlbertEinstein
Here are some concise instructions that should work for you
http://www.cyberciti.biz/faq/psql-fatal-ident-authentication-failed-for-user/.
Basically you need to set the authentication method for localhost to 'trust'.
You need to give "myuser" in postgresql the privilege "Can create database objects".
In pgadmin, it looks like this:
After calling rake db:create, you can take away this privilege.
In my case, I found that a later host permissions rule line in the pg_hba.conf file had over-ridden the earlier local line. I was correctly configuring the local line to use md5 authentication but the host line was set to ident which I changed to md5
As other have emphasized here, note that using trust is an insecure method, so you shouldn't use it in any production-style deployment.

Resources