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"
Related
When I use ssh to run command on a remote machine, I will get the output from shell. However, if I add
sudo su - user2
I will get no output. Now, I cannot do
ssh user2#host
Because of some permission issue.
Is there any way to get the output for the following command?
ssh user1#host 'sudo su - user2; wc -l tmp.txt'
Thanks to #laenkeio. Using sudo -u user2 can run some simple programs.
However, when I need to call a python script which needs some enviroment variable for user2, the script was not able to find those default path by using sudo -u user2.
If you have the appropriate sudo rights on host you should be able to do it with:
ssh -t user1#host 'sudo -u user2 wc -l tmp.txt'
Using sudo -u means "execute as user2", thus avoiding the extra su -. And -t forces ssh to allocate a tty so that sudo can ask for your password.
If you cannot do ssh user2#host for some permission issue, you'll not be able to run ssh user1#host 'sudo su - user2; ... for the same reason...
And, even with no permission issue, when doing su - user you'll be requested for a password...
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.
I'm testing some set ups for EC2 to run geoserver. When launching the EC2 instance I have a bash script to speed things up. However, when it gets to the point of creating a postgres db it fails. Below is an excerpt of the script and it appears to fail after the second line:
chown postgres /usr/local/pgsql/data
sudo su postgres
initdb -D /usr/local/pgsql/data
postgres -D /usr/local/pgsql/data &
exit
yum install gcc make gcc-c++ libtool libxml2-devel -y
# ..... etc etc
I've SSHed into an instance an run the above code manually, then made an AMI from that instance which works. I'd still like to know how to have a bash script for Amazon linux that can also start postgres.
You can't put sudo su postgres in a script to have the subsequent lines be executed by the postgres user. You need to write like:
chown postgres /usr/local/pgsql/data
sudo -u postgres initdb -D /usr/local/pgsql/data
sudo -u postgres -D /usr/local/pgsql/data &
yum install ...
I have installed proftpd file server using apt-get
But after adding User as shown below:
sudo useradd myproftpduser
sudo passwd myproftpduser
sudo usermod -m -d /var/www/ myproftpduser
and after restarting proftpd service I am not able to login with the new user.
I am able to login only with proftpd user.
Not sure where I am making mistake
The right way to create user in proftpd
sudo useradd myproftpduser -d /var/www/myproftpduser -s /bin/false
I can't figure out how to read content of a file from a Docker container. I want to execute content of a SQL file into my PGSQL container. I tried:
docker exec -it app_pgsql psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql
My application is mounted in /usr/src/app. But I got an error:
bash: /usr/src/app/migrations/*.sql: No such file or directory
It seems that Bash interprets this path as an host path, not a guest one. Indeed, executing the command in two times works perfectly:
docker exec -it app_pgsql
psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql
I think that's more a Bash issue than a Docker one, but I'm still stuck! :)
Try and use a shell to execute that command
sh -c 'psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql'
The full command would be:
docker exec -it app_pgsql sh -c 'psql --host=127.0.0.1 --username=foo foo < /usr/src/app/migrations/*.sql'
try with sh -c "your long command"
Also working when piping backup to the mysql command:
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
You can use the database client in order to connect to you container and redirect the database file, then you can perform the restore.
Here is an example with MySQL: a container running MySQL, using the host network stack. Since that the container is using the host network stack (if you don't have any restriction on your MySQL or whatever database), you can connect via localhost and performing the commands transparently
mysql -h 127.0.0.1 -u user -pyour_passwd database_name < db_backup.sql
You can do the same with PostgresSQL (Restore a postgres backup file using the command line?):
pg_restore --host 127.0.0.1 --port 5432 --username "postgres" --dbname "mydatabase" --no-password --clean "/home/dinesh/db/mydb.backup"
Seems like that "docker exec" does not support input redirection.. I will verify this and maybe open an issue for Docker Community at GitHub, if it is applicable.