I am following this tutorial to install Odoo on Ubuntu, but I get this error with command sudo -u postgres createuser -s $USER:
could not change directory to "/home/hugolvc/Documents/Code/odoo/odoo": Permission denied
How can I solve it?
The 'postgres' user doesn't have access to your current work directory, try sudo -iu postgres createuser -s $USER to run the command in a login shell as the user 'postgres' and the message should not appear, since the working directory will then be postgres's home directory.
Afterwards check with sudo -iu postgres psql -c 'SELECT * FROM pg_user' whether the creation of the user was successful.
Related
Im writing a shell to automate a process this shell will be run as root or as another user but not as the postgres user (meaning the user will just run the script)
What i did as a postgres user while testing was
touch /var/lib/postgresql/10/main/recovery.conf
sudo nano recovery.conf (wrote some content )
ctrl +O
However whenever I try to do this by using the following lines in my shell as as another user (including root)
sudo -H -u postgres bash -c "touch /var/lib/postgresql/10/main/recovery.conf"
The file is created as postgres user which is what i intended, and then i run
sudo -H -u postgres bash -c echo "content" > /var/lib/postgresql/10/main/recovery.conf
and get a
-bash: /var/lib/postgresql/10/main/recovery.conf: Permission denied
How come I can create the file but not put content on it?
I already tried giving 775 and 777 permisions using chmod
chmod 775 recovery.conf
as the psotgres user and i get
-rwxrwxr-x 1 postgres postgres 133 May 11 22:11 recovery.conf
to this file as a postgres user and still the error persists so im confused about whats going on
At the second line where you try to put the "echo" command, please use:
/var/lib/postgresql/10/main/recovery.conf
With a / at the beginning
I am able to run psql by doing the following:
Davids-d david$ psql --u postgres
Password for user postgres:
psql (9.4.18)
Type "help" for help.
postgres=#
However, when I run the following command, I get an error:
Davids-iMac:datadocs david$ sudo -u postgres psql -f resources/postgresql/initdb.sql
could not identify current directory: Permission denied
What does this mean, and how would I resolve this? Note that I do have the following var set:
david$ echo $PGDATA
/Users/david/PostgreSQL/data/pg94
The issue is the sudo -u postgres.
Your shell is running as you, but you're running the command as the postgres user. It does not have permission to see the file or even be in the current directory.
We can eliminate psql from the equation by just trying to read the file as the postgres user with sudo -u postgres cat resources/postgresql/initdb.sql. You should get the same error.
There's two things you have to do...
cd to a directory that the postgres user can be in.
Put the file in a place the postgres user can access.
/tmp, for example.
Your command seems wrong, try this:
sudo psql -U postgres -f resources/postgresql/initdb.sql
I just installed postgresql on a Macbook with brew install postgresql. Then I try to psql, but it requires password and then show psql: FATAL: password authentication failed for user "<myname>".
I have not set up anything, and inputting my mac password does nothing. What should I do now?
So your username probably does not exist, as the default username that ships with the db is postgres.
Further, I was prevented from the submission of an empty password, which is blank by default for the postgres user.
You might try
cd ~/
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
Password: YOUR_LOGIN_PWD_HERE (required for sudo)
and then to use
psql -U postgres
password: postgres
I'm not 100% sure of which SO answer I got this from, perhaps here. Hope this helps.
The above did not work for me.
The below steps worked for me:
Step 1: Uninstall Postgres using the following steps:
sudo /Library/PostgreSQL/10/uninstall-postgresql.app/Contents/MacOS/uninstall-postgresql
PS: my postgres version is 10
Step 2: Remove Postgresql user
System Preference > userse & Groups > Unlock > remove postgresql user by clicking "-"
Step 3: Remove existing databases
rm -rf /usr/local/var/postgres/*
Step 4: Install and Start Postgres using brew
brew update
brew install postgresql
brew services start postgresql
Step 5: Create database
initdb /usr/local/var/postgres -E utf8
You can start accessing postgres
psql -h localhost -d postgres
The answer by Pramida almost worked for me... the difference is I was using 9.6 Postgres.
So I ran:
sudo
/Library/PostgreSQL/9.6/uninstall-postgresql.app/Contents/MacOS/installbuilder.sh
and somehow that got rid of my username and almost all of postgres user. I think
I then blew away the directory
sudo rm -rf /Library/PostgreSQL/9.6
And then I installed using brew above.
in my case macboook big sur v 11 you should create /var/postgresql#12 in Mackbook/usr/local
and open terminal in /opt/homebrew/Cellar/postgresql#12/12.8/bin and run
/opt/homebrew/Cellar/postgresql#12/12.8/bin/initdb -D /usr/local/var/postgresql#12
then run in terminal
echo 'export PATH="/opt/homebrew/Cellar/postgresql#12/12.8/bin:$PATH"' >> ~/.zshrc
then
psql -h localhost -p 5432 -d postgres
and enjoy creating user
rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8
I was just following this tutorial HERE, its about, pgrouting, When I run the following command:
psql -U user -d postgres -f ~/Desktop/pgrouting-workshop/data/sampledata_routing.sql
I get an error saying the following:
/var/lib/postgresql/Desktop/pgrouting-workshop/data/sampledata_routing.sql: No such file or directory
On my desktop I do have a folder pgrouting-workshop, which does contain the folder data and the sql dump file.
So why am I getting this error?
Because your Desktop isn't in the postgres user's home directory, located at /var/lib/postgresql, but is instead located at /home/myusername/Desktop?
Presumably the psql command you're running is under a sudo -u postgres -i shell, so ~/ means the postgres user's home directory.
Use ~myusername/Desktop/blahblah. Note that the postgres user may not have permission to access it; you can chmod go+x ~ ~/Desktop (run as your user, not postgres) to change that.
I am having troubles getting started with psql. I can login using the script below
myusername#ubuntu:~/Desktop/dbscripts$ sudo su - postgres
postgres#ubuntu:~$
But, once here, I cannot figure out how to find my .sql file.
I tried the options supplied by Bolo here:
How to import existing *.sql files in PostgreSQL 8.4?
But they only give
myusername#ubuntu:~/Desktop/dbscripts$ psql -U root -d first -f myscript.sql
psql: FATAL: Peer authentication failed for user "root"
and
myusername#ubuntu:~/Desktop/dbscripts$ psql -f myscript.sqlp
sql: FATAL: role "myusername" does not exist
and
myusername#ubuntu:~/Desktop/dbscripts$ sudo su - postgres
postgres#ubuntu:~$ \i myscript.sql
The program 'i' is currently not installed. To run 'i' please ask your administrator to install the package 'iprint'
This can be handled from any of the above options you tried. In which path do you have myscript.sql? After you do su - postgres, give the full path . So it will be psql -d first -f <pathtosqlfile>/myscript.sql. psql -U root will not work unless you have a user root in the database. Try psql -U postgres. You can do \i sqlscript at psql prompt, not at linux command prompt as you have done. The error you are getting "role "myusername" does not exist" can be avoided either by using -U postgres (or any other db user) or by setting the PGUSER environment variable.