"error retrieving current directory" running psql commands under sudo - bash

from a bash script I´m trying to create a postgresql database + users.
It looks like this:
#!/usr/bin/env bash
echo ' Create DBs'
sudo -u postgres createdb -E 'utf-8' -l en_US.utf8 -T template0 testdb
echo ' Create User'
sudo -u postgres psql -c "create role bert with login password 'pass';"
echo ' alter permissions'
sudo -u postgres psql -c "alter database testdb owner to bert;"
unfortuantely I´m getting following errors:
==> default: Create DBs
==> default: could not identify current directory: No such file or directory
==> default: Create User
==> default: shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
==> default: CREATE ROLE
==> default: alter permissions
==> default: shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
I would be very thankful if one coule explain what´s going wrong and how to work around the getcwd error.
thanks!

Your script is being run from somewhere (such as /root -- or possibly a previously-deleted temporary directory) where the postgres user does not have access to the current working directory.
These messages are effectively harmless (unless you're invoking scripts that depend on $PWD or similar variables or commands), but if you want to avoid them, change directories to somewhere certain to exist and which the postgres user has access to. For instance, put the line:
cd /
...at the top of your script, just under the shebang.

Related

Read-only file system when attempting mkdir /root/.local/share on Mac with Putty

I am trying to start Bitnami AWS with Putty in mac, but when i start Auth in SSH with both Catalina and Big Sur i get this error:
(putty: 3637): Gtk-WARNING **: Attempting to set the permissions of `/Users/daniele/.local/share/recently-used.xbel ', but failed: No such file or directory
I tried to install the folder:
sudo mkdir -p /root/.local/share
I get this error:
mkdir: /root/.local/share: Read-only file system
As per the error message, we should create the folder at the following path:
/Users/daniele/.local/share/
And not:
/root/.local/share
Therefore, the correct command is:
mkdir -p /Users/Daniele/.local/share
Require the result in command: csrutil status
If result is enabled, you need to restart machine and press command + R, open the terminal in the recovery, so input csrutil diabled.
Restart, and check the status: csrutil status.
Here are two methods:
you are root.
sudo mount -uw /
so, you could mkdir or touch new file.
If you still can't read or write any, you maybe try this:
cd ~ # cd home directory
sudo vim /etc/synthetic.conf # create new file if this doesn't exist
In the conf files, add new line
data /User/xx/data # Notice: the space between this two strings is tab
Restart, and you will find a link named /data in the root.

PostgreSQL: execute query from script

I'm installing PostgreSQL + POSTGIS on a CentOS 7 virtual machine using Vagrant and Virtual Box.
My Vagtantfile is the follow ...
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.network "private_network", ip: "192.168.56.2"
config.vm.provider "virtualbox" do |vb|
vb.memory = "4096"
vb.name = "Test"
end
config.vm.provision "shell", path: "./scripts/InstallPostgresqlPostgis.sh"
end
In ./scripts/InstallPostgresqlPostgis.sh there are all the commands to install PostgreSQL and, when run, PostgreSQL is installed and works.
To add POSTGIS at my PostgreSQL installation, in interactive way, I use this procedure
su postgres
----->>>>>>> HERE I'VE TO PUT THE USER PASSWORD <<<<<<<-------
psql
-- Enable PostGIS (includes raster)
CREATE EXTENSION postgis;
-- Enable Topology
CREATE EXTENSION postgis_topology;
-- Enable PostGIS Advanced 3D
-- and other geoprocessing algorithms
-- sfcgal not available with all distributions
CREATE EXTENSION postgis_sfcgal;
-- fuzzy matching needed for Tiger
CREATE EXTENSION fuzzystrmatch;
-- rule based standardizer
CREATE EXTENSION address_standardizer;
-- example rule data set
CREATE EXTENSION address_standardizer_data_us;
-- Enable US Tiger Geocoder
CREATE EXTENSION postgis_tiger_geocoder;
\q
and all works.
I've to "translate" this procedure in my InstallPostgresqlPostgis.sh that I refer in my Vagrantfile and I've tried this
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION postgis"
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION postgis_topology"
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION postgis_sfcgal"
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION fuzzystrmatch"
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION address_standardizer"
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION address_standardizer_data_us"
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION postgis_tiger_geocoder"
but the result is ...
default: could not change directory to "/home/vagrant": Permission denied
default: CREATE EXTENSION
default: could not change directory to "/home/vagrant": Permission denied
default: CREATE EXTENSION
default: could not change directory to "/home/vagrant": Permission denied
default: CREATE EXTENSION
default: could not change directory to "/home/vagrant": Permission denied
default: CREATE EXTENSION
default: could not change directory to "/home/vagrant": Permission denied
default: CREATE EXTENSION
default: could not change directory to "/home/vagrant": Permission denied
default: CREATE EXTENSION
default: could not change directory to "/home/vagrant": Permission denied
default: CREATE EXTENSION
Where am I doing wrong?
Your problem is that you are executing the commands with a working directory
that is not accessible to postgres user. In fact it is the home directory of the user executing the commands (vagrant).
There are three approaches for fixing this issue:
use --login (or -i for short) option to sudo
This will cause sudo to execute the commands with settings similar to a login shell.
Especially this will (try) changing to the target user's home directory as a working directory.
change the working directory within your script using cd ~postgres
This will result in all sudo commands will being executed there.
Allow user postgres access to the home directory of user vagrant
THIS IS DANGEROUS AND ABSOLUTELY NOT RECOMMENDED!!!
I just mention it for completeness. It might be an option iff
you need such access regularly
and you have some fine grain access control at hand (e.g. ACL)
that allows ensuring postgres really is the only user being granted access.
Even then you should think thrice!
In most cases alternatives 1. or 2. are to be preferred.
I've solved in this way ...
sudo su postgres
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION postgis"
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION postgis_topology"
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION postgis_sfcgal"
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION fuzzystrmatch"
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION address_standardizer"
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION address_standardizer_data_us"
sudo -u postgres -H -- psql -d postgres -c "CREATE EXTENSION postgis_tiger_geocoder"

Execute commands in shell using root user

I am trying to execute a command in remote machine through shell script. That command needs to be executed as a root user. After logging in to remte machine with my normal ID, then execute the command(which is there is different path) in a specific path with root user. I am using below code
current_dir=$PWD;/usr/local/bin;sudo -u root drush data_export_import-export nodes --content-types=book;cd $current_dir;
i am getting below error
./test.sh: line 8: /usr/local/bin: is a directory
[sudo] password for s57232:
PHP Warning: Module 'pgsql' already loaded in Unknown on line 0
PHP Warning: Module 'pgsql' already loaded in Unknown on line 0
The drush command 'data_export_import-export nodes' could not be found. Run `drush cache-clear drush` to clear the commandfile cache if you have installed new extensions. [error]
It should not expect the password to be supplied and also drush command need to be executed from /usr/local/bin in /var/www/html path.
i tried below also but did not work
sudo -u root /var/www/html
sudo -u root /usr/local/bin/drush data_export_import-export nodes --content-types=book >> ${DATAPATH} 2>&1
can anyone help me?

Unix script can't alter postgres hba.conf configuration file on Ubuntu

I'm attempting to setup postgres 9.6 on ubuntu/vagrant through a provisioning script. Part of my script adds a line to pg_hba.conf with the following command:
sudo -u postgres echo "host all all all md5" >> /etc/postgresql/9.6/main/pg_hba.conf
However, this gives me the error -bash: /etc/postgresql/9.6/main/pg_hba.conf: Permission denied
Which is strange because I am allowed to edit the file with either sudo nano or sudo -u postgres nano.
Here are the permissions on the file:
-rw-r----- 1 postgres postgres 4641 Apr 6 16:11 pg_hba.conf
How can I add this line to my configuration file in a script?
The problem here is that redirection happens before command execution. So the redirection doesn't have the elevated privileges you expected it to.
There's more than one way around that problem. I generally use something like this.
echo "host..." | sudo tee -a /etc/postgresql/9.6/main/pg_hba.conf
Piping to sudo tee... avoids problems with quoting.
How bash executes commands
Redirections

"sudo p4 sync -f" results in "User root doesn't exist"

I'm trying to do p4 sync -f on an ubuntu Vagrant box, but that just gives me a stream of "open for write: [filename]: Permission denied". If I do sudo p4 sync -f, I immediately get the error "User root doesn't exist." How should I go about debugging this? This problem does not exist when I use sudo with other commands.
Do:
sudo p4 -u YOURUSERNAME sync -f
so that the Perforce command is executing under your Perforce user name rather than "root".
Alternatively:
set P4USER in your environment explicitly
chown/chmod the files so you can write to them without sudo
create (and give permission to) a Perforce user named "root"

Resources