invalid multibyte char (US-ASCII) in a chef recipe - ruby

Im trying to do a postgresql base back up via bash chef resources. Below is my code and im getting the following error. When i deploy them. Any thoughts why.
$PGDATA = "node['fc_db']['postgres']['pg_data']"
bash "backup master db" do
user "postgres"
code <<-EOH
initdb –D –-no-locale –-encoding=UTF8
pg_ctl –D #{$PGDATA} start
psql –c "SELECT pg_start_backup('initial backup for SR')" template1
tar cvf pg_base_backup.tar #{$PGDATA}
psql –c "SELECT pg_stop_backup()" template1
EOH
end

Which version of Ruby are you using? If not 2.x.x, try adding # encoding: UTF-8 on top of your wal-e.rb?

The line in the code :
initdb –D –-no-locale –-encoding=UTF8
Should be:
initdb –D --no-locale --encoding=UTF8

Related

Importing a database dump -- PSQL

I am trying to import an old .gz database dump into my database using the terminal. It is a Postgresql environment.
This is what i am doing:
psql test < 052710_1.gz
Responce:
ERROR: syntax error at or near "test"
LINE 1: test
^
I also tried:
psql --dbname test < --file 052710_1.gz
psql -d test -U postgres -f 052710_1.gz
And they both gave me the same error.
I have tried using the .exe on the end of psql and it has the same issue.
I am running Postgresql 10.1
For the case of version 10.1
try using the following command
pg_restore -d test < 052710_1.gz
Hope this works.
Please follow the commands. Hope it will works.
sudo psql -U postgres
 create database temp_databse;
 exit
Execute following command
 
psql -U postgres temp_database < extracted_database_file_name
 Above command will restore database...

Why command -- sh raise error?

Why the shell command raises error:
sudo -u postgres \
-- sh -c '/usr/bin/env psql -c "CREATE ROLE deploy PASSWORD secret SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;"'
Causes:
ERROR: syntax error at end of input
LINE 1: CREATE
^
What's right syntax ?
Many thanks.
Try:
sudo -u postgres '/usr/bin/env RBENV_ROOT=/usr/local/rbenv RBENV_VERSION=2.1.3 psql -c "CREATE ROLE deploy PASSWORD secret SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;"'
env takes variable assignments before the command to run. And I don't see why you need to use sh -c, sudo executes the command for you.

PostgreSQL Command fom bash script

I work with PostgreSQL 9.2 and bash script. I have a problem.
My DB is TestDB ,
Table name is config
and my bash code is
sudo -u postgres psql -c "select count(*) from public.config where var_name='url'"
result is
ERROR: relation "public.config" does not exist
Can anybody help me?
sudo -u postgres psql TestDB -c "select count(*) from config where var_name='url'
The Database was missing, so you executed the command into postgres db where you probably don't have that table ;-)

How to use not_if in a chef recipe

I am new to chef so I am a little confused in how the conditional not_if works inside a execute resource. I understand that it tells chef not to execute a command if the command returns 0 or true; however, in my code it is apparently still running the command.
The following code is supposed to create a user (and its password) and a database; however, if the user and database already exist, it should not do anything. The user, database and password are defined in the attributes. The following is the code I have:
execute "create-user" do
code = <<-EOH
psql -U postgres -c "select * from pg_user where usename='#{node[:user]}'" | grep -c #{node[:user]}
EOH
user "postgres"
command "createuser -s #{node[:user]}"
not_if code
end
execute "set-user-password" do
user "postgres"
command "psql -U postgres -d template1 -c \"ALTER USER #{node[:user]} WITH PASSWORD '#{node[:password]}';\""
end
execute "create-database" do
exists = <<-EOH
psql -U postgres -c "select * from pg_database WHERE datname='#{node[:database]}'" | grep -c #{node[:database]}}
EOH
user "postgres"
command "createdb #{node[:database]}"
not_if exists
end
Chef gives me the following error:
Error executing action run on resource 'execute[create-user]'
...
[2013-01-25T12:24:51-08:00] FATAL: Mixlib::ShellOut::ShellCommandFailed: execute[create-user] (postgresql::initialize line 16) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
STDERR: createuser: creation of new role failed: ERROR: role "user" already exists
To me it seems that it should work;however, it still running the execute. Am I missing something?
Thank you
I had been having the same issue. But, in my case, "not_if" seems executed by different user (root), and failed to check the condition properly. Adding [:user => "postgres"] resolved the issue.
execute "create-database-user" do
user "postgres"
exists = <<-EOH
psql -U postgres -c "select * from pg_user where usename='#{settings[:username]}'" | grep -c #{settings[:username]}
EOH
command "createuser -U postgres -sw #{settings[:username]}"
not_if exists, :user => "postgres"
end
I've referred the following code example.
https://github.com/MarcinKoziuk/chef-postgres-dbsetup/blob/master/recipes/default.rb
You're checking for the existence of:
node[:user]
If it doesn't exist, you create:
node[:postgresql][:user]
Unless these happen to be equal, you'll keep trying to create node[:postgresql][:user] repeatedly.
First, there is a typo in the WHERE condition. It should probably be username instead of usename.
Anyawy, you should do:
execute "create-user" do
user "postgres"
command "createuser -s #{node[:user]}"
not_if "psql -U postgres -c \"select * from pg_user where username='#{node[:user]}'\" | grep -c #{node[:user]}"
end
This assumes that your psql -U postgres -c "select * from pg_user where username='#{node[:user]}'" is correct.
Same with a database:
execute "create-database" do
user "postgres"
command "createdb #{node[:database]}"
not_if "psql -U postgres -c \"select * from pg_database WHERE datname='#{node[:database]}'\" | grep -c #{node[:database]}}"
end
Regarding the username, even if it already exists, changing the password to the known one shouldn't cause a problem. After all you know the password.
FYI, you can define multiple conditionals within one resource.
Good luck with Chef! I love it very much!

How to check status of PostgreSQL server Mac OS X

How can I tell if my Postgresql server is running or not?
I'm getting this message:
[~/dev/working/sw] sudo bundle exec rake db:migrate
rake aborted!
could not connect to server: Connection refused
Is the server running on host "localhost" and accepting
TCP/IP connections on port 5432?
Update:
> which postgres
/usr/local/bin/postgres
> pg_ctl -D /usr/local/bin/postgres -l /usr/local/bin/postgres/server.log start
pg_ctl: could not open PID file "/usr/local/bin/postgres/postmaster.pid": Not a directory
Update 2:
>pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
server starting
sh: /usr/local/var/postgres/server.log: No such file or directory
The simplest way to to check running processes:
ps auxwww | grep postgres
And look for a command that looks something like this (your version may not be 8.3):
/Library/PostgreSQL/8.3/bin/postgres -D /Library/PostgreSQL/8.3/data
To start the server, execute something like this:
/Library/PostgreSQL/8.3/bin/pg_ctl start -D /Library/PostgreSQL/8.3/data -l postgres.log
You can run the following command to determine if postgress is running:
$ pg_ctl status
You'll also want to set the PGDATA environment variable.
Here's what I have in my ~/.bashrc file for postgres:
export PGDATA='/usr/local/var/postgres'
export PGHOST=localhost
alias start-pg='pg_ctl -l $PGDATA/server.log start'
alias stop-pg='pg_ctl stop -m fast'
alias show-pg-status='pg_ctl status'
alias restart-pg='pg_ctl reload'
To get them to take effect, remember to source it like so:
$ . ~/.bashrc
Now, try it and you should get something like this:
$ show-pg-status
pg_ctl: server is running (PID: 11030)
/usr/local/Cellar/postgresql/9.2.4/bin/postgres
As of PostgreSQL 9.3, you can use the command pg_isready to determine the connection status of a PostgreSQL server.
From the docs:
pg_isready returns 0 to the shell if the server is accepting connections normally, 1 if the server is rejecting connections (for example during startup), 2 if there was no response to the connection attempt, and 3 if no attempt was made (for example due to invalid parameters).
You probably did not init postgres.
If you installed using HomeBrew, the init must be run before anything else becomes usable.
To see the instructions, run brew info postgres
# Create/Upgrade a Database
If this is your first install, create a database with:
initdb /usr/local/var/postgres -E utf8
To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
Once you have run that, it should say something like:
Success. You can now start the database server using:
postgres -D /usr/local/var/postgres or
pg_ctl -D /usr/local/var/postgres -l logfile start
If you are still having issues, check your firewall. If you use a good one like HandsOff! and it was configured to block traffic, then your page will not see the database.
It depends on where your postgresql server is installed. You use the pg_ctl to manually start the server like below.
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
You can use brew to start/stop pgsql. I've following short cuts in my ~/.bashrc file
alias start-pg='brew services start postgresql'
alias stop-pg='brew services stop postgresql'
alias restart-pg='brew services restart postgresql'
The pg_ctl status command suggested in other answers checks that the postmaster process exists and if so reports that it's running. That doesn't necessarily mean it is ready to accept connections or execute queries.
It is better to use another method like using psql to run a simple query and checking the exit code, e.g. psql -c 'SELECT 1', or use pg_isready to check the connection status.

Resources