Setting postgresql password with Vagrant/Chef-solo - postgresql

I'm trying my hand at a bit of chef-solo for the first time on a Vagrant box for my open source project apartment so that contributors can more easily get a dev env up and running.
One thing I'm as of yet unable to achieve though is override the default password set by the postgresql recipe from this cookbook
The docs mention setting the json config with something like:
postgresql: {
password: {
postgres: ''
}
}
which I've done here but I can't seem to get it to override the default encrypted password that's set by the recipe.
I'm brand new (as of today) to chef/vagrant so I'm a bit lost as to where I should even look next.
Any help is greatly appreciated.

I cloned your gem and fired up Vagrant (but using a Debian Squeeze image I already downloaded and with all recipes disabled except postgresql and postgresql::server, and using PostgreSQL 8.4 instead of 9.1) and it seems that the empty password was a bad choice, using
postgresql: {
password: {
postgres: 'foo'
}
}
works with the command PGPASSWORD="foo" psql --username=postgres -h localhost so I suspect that the empty password has some problems.

Related

Error while using createdb command postgresql on windows 10

I am a new user of PostgreSQL, and I am learning it so that I can use it with Django, so I am trying to use the createdb command to create a new database, but even though I am entering the correct password for my account, I am getting this error. I reinstalled everything and checked everything I could think of but I was not able to solve this error. So, if I can get some help regarding this issue it would be nice.
Even using the command psql, and submitting the correct password, gives the same error.
I am using Windows 10.
As far as I checked, I needed to enter the password I used while installing PostgreSQL.
By the way, I am using the latest version of PostgreSQL 14.1
The command I used:
createdb testdatabase
createdb: error: connection to server at "localhost" (::1), port 5432 failed:
FATAL: password authentication failed for user "<username_placeholder>"
So, basically, I figured the solution myself. I am just posting it here because mostly answers are available for Linux and not Windows. So, if a windows user has a similar problem, maybe this answer could help them.
So, the first thing is, if you need to open psql, use the command:
psql -U postgres
and then enter the password you used while installing PostgreSQL. Now, if you wish to do something similar to what I tried, what I mean is to use createdb command in the terminal itself, then you will have to create a new user using the same username as you do for your PC, like in my case, it is aryan.
(For example: C:\Users\aryan\).
I followed instructions from this website.
I personally used pgAdmin 4 to do it, you could also use the SQL commands themselves.
After doing everything, when I used the createdb command directly from the terminal/powershell, it asked my the password which I had used to create the other user( with the same username as my system/pc) using pgAdmin 4. That's it. This helped me out.

AWS Postgres DB "does not exist" when connecting with PG

I can't seem to connect to my DB instance in AWS. I'm using the pg package and following the examples from the website is not working.
A search for "aws postgres database does not exist" really isn't returning anything helpful. Going through the open/closed issues on the PG github isnt helpful either.
Running $nc <RDS endpoint> <port number> returns a success message so it's definitely there. Every value placed in the Client config is copy/pasted from my DB instance.
I'm starting to wonder if the databases have a different name than what it shows in the "Instances" section of RDS on AWS?
const client = new Client({
host : '<<RDS ENDPOINT>>',
database : '<<RDS NAME>>', // maybe this isnt the real name?
user : '<<username>>',
password : '<<password>>',
port : <<port>>
});
client.connect()
.then(data => {
console.log('connected');
})
.catch(err => {
console.log(err);
})
I ran into this issue as well. It looks like the DB Instance Name and the actual DB name are two different things and even when you add a name when you create your DB, it defaults to 'postgres'. When I put in the name of my DB it gave me the same error. However, when I just put in 'postgres' it worked fine. Try that and see if it works for you.
The initial configuration of RDS instances is quite messy, since the parameter "database name" is only the name of the instance, not the proper name of the database. If you want AWS to create a database at the moment you create the db instance, you have to select "Additional configuration" and explicitly add a parameter called "Initial database name". Check the screenshot I attach here.
Try adding postgres as dbname. It worked for me!
After connecting with postgres as db name, you can type \l to list all database on that PSQL cluster, that will return a bunch of default dbs and also the one you created (the name) so you can connect to it
I ran into the same issue after creating a DB instance on AWS RDS. I wanted to test the connection of my database using PostBird, and I used my actual DB instance name but it could not work.
But I used "postgres in field of DB_name and it worked. That means that my default username was posgres and db_name was also "posgres.
I hope it will help you too.
Try this if the above answer does not work.
Remove the:5439/lab ending so that the Host value ends with: .com

Ansible 2.2 Create Postgresql Database

I am new to ansible (2.2.1) and have started to migrate from our fabric scripts to ansible which I find somewhat better regarding structure. I have run into an issue, it should be pretty straight forward but since I do not know ansible through and through I am not sure how to proceed. I am running this against a vagrant box as of now.
The issue is regarding user privileges and postgres.
Lets say I have this playbook
- hosts: web
become: yes
become_user: root
vars:
dbname: myapp
tasks:
- name: ensure database is created
postgresql_db: name={{dbname}}
I cannot make this simple example work! All dependencies are met. If I do the same thing with mysql this works fine but here I get issues with unable to connect to database: FATAL: Peer authentication failed for user "postgres".
In mysql I use the "root" user with a blank password, which works because I know that user is created upon install with a blank password.
There is a user postgres created when the installation of postgresql is completed so the user exists. And as root I should be able to login by saying I am the postgres user. Am I missing something in how this is done? It works just fine if log into the server and sudo -su postgres && psql.
I also tried to add become_user: postgres by the task I want to run but then I get unprivileged user issues.
Any ideas of what is missing?
Found a few workarounds and a solution, given you are ok with giving away some security (which makes sense, this being an issue just for that reason).
More people having this problem with the new ansible in this github issue thread
https://github.com/ansible/ansible/issues/16048
What I ended up doing was settings allow_world_readable_tmpfiles = True in the ansible.cfg file. Then this is not an issue anymore but you get warnings. I only need this setting for when handling the postgres role, so maybe I'll end up split it up or putting the setting somewhere less global.

Knex:Error Pool2 - error: password authentication failed for user

I am having trouble with my migration to Ubuntu Linux. I can use Postgres in the terminal. So I don't have a problem with the Postgres password.
When I type: knex migrate:latest --env development
I get:
Using environment: development
Knex:warning - Pool2 - Error: Pool was destroyed
Knex:Error Pool2 - error: password authentication failed for user "user"
I've read from other answers in related questions to go into the pg_hba.conf and set the method to trust. I've done this, but no change.
my knex.js file looks like this:
module.exports = {
devolopment: {client: 'pg',
connection: 'postres://localhost/bikesdb'
},
production: {
client: 'pg',
connection: process.env.DATABASE_URL
}
};
I'm not sure what I'm doing wrong.
You are most likely running on node 6.x.x so you'd need to upgrade your pg package version npm install --save pg#4.5.5
Reference: https://github.com/tgriesser/knex/issues/1371
If Andrei Stalbe's solution doesn't help... here are some more thoughts.
How are you connecting to postgres from commandline?
If you are writing just psql bikesdbit actually uses unix socket for connecting and it has different security settings in postgres by default than through TCP. You can try to change your connection string to: postgres:///bikesdb which makes also knex to use unix socket.
You can change/check security policies in pg_hba.conf.
Also you have typo in postres://localhost/bikesdb.
EDIT:
I'm pretty sure you are still having some general postgresql configuration problem. If you like to create user to access your database you can do this:
CREATE ROLE bikesuser WITH LOGIN PASSWORD 'letmepass';
GRANT ALL PRIVILEGES ON DATABASE "bikesdb" TO bikesuser;
And change connection string to:
'postgres://bikesuser:letmepass#localhost/bikesdb'
If you like to allow access from localhost to postgresdb this should do it in pg_hba.conf
host all all 127.0.0.1/32 trust
Hope that something of this is useful, even that it sounds that you have already tried this all.
I had the same problem and it was all about the fact that knex was trying to use the username taken from the environment variables (LOGNAME) of my command line interface.
If Postgres is set to use a different user (by default postgres if you haven't changed it) there will be an authentication error.
You can solve this problem by specifying the authentication credentials inside the URL that you use to connect to the Postgres server.
module.exports = {
devolopment: {client: 'pg',
connection: 'postres://user:password#localhost/bikesdb'
},
production: {
client: 'pg',
connection: process.env.DATABASE_URL
}
};
I highlight the key point in the URL:
postres://user:password#localhost/bikesdb
You can check the name of the user to access the Postgresql server from the UI interface provided by Postgresql and the password is the one that you have defined during the installation phase (if you haven't changed it)
I ran into this problem recently. My issue was I could run migrations on local macOS but not on a remote Linux Ubuntu machine. I found out that the reason was I didn’t have a password set for the user, though everything works fine on local for some reason it required a password on remote (Linux). So, setting password for the user and adding the password to the connection solved my problem.
Example:
On postgres
\password username
Then enter and confirm the password.
In your config file (e.g knexfile.js)
module.export = { client: ‘postgresql`, user: username, password: secret }
I had set my psql to go to my user instead of postgres. However, I reset the postgres password. I just reset the postgres password, and the in the development connection I included the password. (I tried including it before the postgres password reset and it still didn't work). It's odd because the user I specified in the connection was me and not postgres.

heroku pg:pull password authentication failed

I'm trying to run the heroku pg:pull command, but I can't seem to get the amazingly cryptic authentication process.
The command I'm running:
> heroku pg:pull app_name::RED localdb
I then get a password prompt, which I can't, for the life of me, figure out. After 2 guesses I get password authentication failed for user "Hanan", and that's it.
I tried Heroku's password, my Windows account password, every password I use, but nothing happens. I checked, and "Hanan" is not a role in Postgresql, so trying to change the password through psql doesn't work. I have no problem logging in to Postgresql through other roles, but it's this 'default' log-in process which I can't seem to crack.
Also, since I'm using windows, I'm not sure how to run commands like sudo -u postgres psql, which I see as a possible solution.
Will appreciate any help regarding this issue, I'm really frustrated by now...
Apparently it's possible to set the environment variables PGUSER and PGPASSWORD, as described here.
However, this won't work on windows in the given syntax. To do this on windows run the following:
SET PGUSER=[pg_username]
SET PGPASSWORD=[pg_password]
after entering these two lines Postgres will log you in with the given authentication info, instead of trying to sign in with the windows username
I've run into this problem a lot when running heroku pg:pull. The issue in my case was that the pg:pull command only works if my local PostgreSQL server has a password set.
To set a password, run psql localdb and execute this SQL:
ALTER USER my_user_name with password 'my_new_password';
(You won't necessarily be required to use this password all the time. Run psql localdb and see whether you're prompted; in my case, I can still log in to psql without the password.)
Now run heroku pg:pull --app my_heroku_app POSTGRESQL_COLOR localdb, and enter your new password (twice) when prompted.
I'm using Windows 10, 64-bit, Powershell and had to use the following commands to properly set the local PostgreSQL environment variables:
C:\> $Env:PGUSER="[pg_username]"
C:\> $Env:PGPASSWORD="[pg_password]"
To verify that these are set properly, list all local environment variables with this:
C:\> Get-ChildItem Env:
After doing this, I was able to run heroku pg:pull without being prompted for a password.
The previous answers did not work for me or were not to my liking so I kept searching. Thanks to the answer provided by Rayz on this post How to add a user to PostgreSQL in Windows? I was able to come up with this one liner for windows powershell.
& { $env:PGUSER="username";$env:PGPASSWORD="password"; heroku pg:push local-db DATABASE_URL --app heroku-app}
You apparently have to pass the variables as a list seperated by semicolons, surrounded by braces which are preceeded by an ampersand. Your funtion (heroku pg:pull/ pg:push/...) has to be a member of the list. As of my current testing it works in powershell with pg:push and the order of items within the braces does not matter.
I was too lazy to find out how to change credentials used by heroku. SET PGUSER and SET PASSWORD did not work for me, what i did was this:
Error said invalid credentials for "janbr" so i have created the user in the local db with a lot of priviledges. I used DBeaver for that with postgres credentials i have set up upon instalation of postgres.
Not a very clean solution though.
Using Windows 10, I tried all of the solutions here without any luck.
What ended up working for me was going into System Properties and editing my Environment Variables there. I added these to my System Variables, restarted my terminal and was able to run after that. One thing to note is that I first tried to add user and password to my User Variables but that didn't work.
PGUSER YourPostgresUser
PGPASSWORD YourPassword
I also ran Get-ChildItem Env: after updating my variables to check that they had been added.