I am trying to a create a user in postgres, I did the following.
sudo -u postgres createuser mystore
But I found out that I should use -s flag while creating the user, So my question is what is role of -s flag while creating user.
And I tried to remove the user by the following steps
sudo -u postgres psql
drop user mystore
Then tried to create the store with the -s flag, it says
role "mystore" already exists.
How to handle this
createuser -s will give the new user superuser privileges. As with most command-line tools in Linux, you can get a description of each flag by running createuser --help.
The problem in psql appears to be a missing semicolon after your drop command. psql supports multi-line statements, so hitting Enter will simply add a new line; it won't submit the command to the server until it sees a semicolon terminator.
Related
I have loged in postresql as default postgres user:
psql -U postres
and following the official documentation: https://www.postgresql.org/docs/12/app-createuser.html I have type this:
create user -d -e --role=myrole -r -s myuser;
and nothing happens. Postresql ignores the command.
\du
does not return myuser in user list.
It ignores the command when added any option, only this works:
create user myuser;
CREATE USER is a SQL statement that has its own syntax https://www.postgresql.org/docs/12/sql-createuser.html whereas createuser is an executable that has a different syntax: https://www.postgresql.org/docs/12/app-createuser.html: you cannot mix them.
I followed this tutorial and made a typo where I was supposed to create a user for my django apps to connect as;
I was supposed to run su - postgres -c "createuser www-data -P" but I ran su - postgres -c "createuser www-dtata -P".
I dont want to proceed until I remove that user, which I don't know the command for. I found and tried DROP USER after searching around, but the terminal returned -su: DROP: command not found.
Run sudo su - postgres -c "dropuser www-dtata"
You can use dropuser console tool (see https://www.postgresql.org/docs/current/static/app-dropuser.html):
su - postgres -c "dropuser www-dtata"
Or use DROP USER SQL query (see https://www.postgresql.org/docs/current/static/sql-dropuser.html):
sudo -u postgres psql -c 'DROP USER "www-dtata";'
These 2 approaches do the same thing. In SQL version, you also need to use double quotes around DB user name, due to - in it.
First run the command
sudo su
Enter the user password for root access.
Then run the below command
su - postgres -c "dropuser www-dtata"
No password will be prompted
I have a series of deletes and updates on a few tables in a Postgres database I manage. It has been suggested to schedule a reindex after the series of deletes as a solution to the 10 minute next-step update freezing infinitely (as it randomly does.) The DOS instructions provide this:
Usage:
reindexdb [OPTION]... [DBNAME]
Options:
-a, --all reindex all databases
-d, --dbname=DBNAME database to reindex
-e, --echo show the commands being sent to the server
-i, --index=INDEX recreate specific index only
-q, --quiet don't write any messages
-s, --system reindex system catalogs
-t, --table=TABLE reindex specific table only
--help show this help, then exit
--version output version information, then exit
Connection options:
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port
-U, --username=USERNAME user name to connect as
-w, --no-password never prompt for password
-W, --password force password prompt
We have to use version 9.1.3 as this is the corporate standard.
I have tried every option I can think of but it won't take the command to reindex:
reindexdb.exe -U username=MyUserName -W MyPassword -t table=MyDatabase.MyTable
I've also tried
reindexdb.exe -U MyUserName -W MyPassword -t MyDatabase.MyTable
and
reindexdb.exe -U MyUserName -W MyPassword -t MyTable -d MyDatabase
...but they all end with the error:
reindexdb: too many command-line arguments (first is "-t")
Does anyone have a working sample that would be able to clarify what the right syntax is?
Remove MyPassword from your arguments, and enter it in when Postgres prompts you for it.
-W simply causes Postgres to prompt for the password; it doesn't accept the password itself. You should never specify passwords on the command line, as it's usually logged.
If you need to run it non-interactively, either set the PGPASSWORD environment variable or create a pgpass file.
This did it:
reindexdb.exe -d MyDatabase -U postgres -t MyTable
As #Colonel Thirty Two and #Erwin Brandstetter noted, removing the password entirely is possible through %APPDATA%\postgresql\pgpass.conf
Any of these can be forced by adding the keyword FORCE after the command
Recreate a single index, myindex:
REINDEX INDEX myindex
Recreate all indices in a table, mytable:
REINDEX TABLE mytable
Recreate all indices in schema public:
REINDEX SCHEMA public
Recreate all indices in database postgres:
REINDEX DATABASE postgres
Recreate all indices on system catalogs in database postgres:
REINDEX SYSTEM postgres
link
I'm trying to build a setup script to automate the development environments creation, but I'm having trouble both trying to pipe or using the -c modifier for psql.
I've tried:
sudo su postgres psql -c "ALTER ROLE postgres WITH password 'pass'"
and
sudo su postgres psql -c "ALTER ROLE postgres WITH password 'pass';"
Both of which say "ALTER: command not found"
I've also tried pipe, but I'm not able to combine it with su correctly
eg: I tried something like
sudo su postgres echo "ALTER ROLE postgres WITH password 'pass'" | psql
But postgres can't execute "echo"
And:
echo "ALTER ROLE postgres WITH password 'pass'" | sudo su psql
Which just doesn't work.
So, my first question is: how can I execute this simple command from a sh file?
And the second one, less related: how can I use different users in the commands chained with pipe?
What's wrong is the lack of -c or --command for su to indicate that the rest of the line is a command.
But su is not needed anyway, because there's already sudo. Do this instead:
sudo -u postgres psql -c "ALTER ROLE postgres WITH password 'pass'"
If you are just like me, make sure you are not running this from:
bash-4.2$
but:
postgres=#
During the installation of my app, I want to create a PostgreSQL-Database and some tables and functions.
For that purpose I use PSQL.EXE that ships with PostgreSQL. I have 2 scripts. The first one creates the database and a corresponding user that has rights to execute scripts on that database. I want to execute the second script as this just created user. Unfortunately I can't find a way to pass the password for that user as a command line argument. Omitting the password leads to a stop of execution and a prompt for the user to enter the password, which I would like to avoid - since this is executed during installtion of my app.
Is there any way to pass the password as argument or is there any other command line tool I could use?
To explain the environment a bit further. I use WiX 3.5 setup as a "MSI-Builder".
You can either use a pgpass file as dbenhur answerd, or you can set the environment variable PGPASSWORD before calling psql:
SET PGPASSWORD=my_very_secret_password
psql somedb someuser
All supported environment variables are documented in the manual: http://www.postgresql.org/docs/current/static/libpq-envars.html
You can't supply password via cmdline arg (and don't want to as that's poor security practice).
You can provide a .pgpass file to support automatic script authentication. Here's the docs.
Better still, if you have access to create the db role then you already have all the access you need without having to carefully log in with a password. Have the second script operate under the same user as the first but include the following line to switch user:
set role my_new_user;
Where my_new_user is the name of the role you want to run it as.
If you only divided the scripts because of the different logins then with this they can go in the same file and just switch role mid way through.
Note:
On the off chance that you are not creating the DB and new role as a super user this may be a little more complex. If this is the case you will need to create the new role with:
create role my_new_role ... ADMIN my_role;
Where my_new_role is the role you're creating and my_role is your current user. Then when you're finished simply:
revoke my_new_role from my_role;
For completion, you can also use URI (doc link)
List dbs
psql "postgresql://username:password#localhost/postgres" -l
I also crafted this command to have only names (please tell me if you know a better way):
psql "postgresql://username:password#localhost/postgres" -l | awk -F '|' '{print $1}'| sed -e '/^\s*$/ d' -e '1,3d'|sed '$d'|awk '{print $1}'
You can also use unix socket to connect:
# ss -x -a |grep postgres|awk '{print $5}'
/var/run/postgresql/.s.PGSQL.5432
Note that the parent directory of the socket is used:
# sudo -u postgres psql -d "postgresql:///postgres?host=/var/run/postgresql/" -l
You can only do this if you have this line in your pg_hba.conf:
local all postgres ident
"ident" uses unix user for authent
dump a db
Here I added a different port number
pg_dump -Fc "postgresql://username:password#localhost:9001/${db}" > "backup_${db}.pgdump"
With dumpall you need a super user or role (with CREATE ROLE ... SUPERUSER). And it must have access to all DB. By default postgres can.
but in my case I couldn't use pg_dumpall with postgres because his password was removed by devs.
So I used:
sudo -u postgres pg_dumpall -d "postgresql:///?host=/var/run/postgresql/" > all.dump
tested version
# cat /opt/postgresql/PG_VERSION
9.6
hth