Postgres: Create role in one line - postgresql

I want to create a role/user in one line. Here's what I tried:
psql -U postgres -c 'createuser my_app with createdb login password 'my_password';'
psql: warning: extra command-line argument "with" ignored
psql: warning: extra command-line argument "createdb" ignored
psql: warning: extra command-line argument "login" ignored
psql: warning: extra command-line argument "password" ignored
psql: warning: extra command-line argument "'my_password';'" ignored
psql: FATAL: database "my_app" does not exist
In place of createuser I have also tried create user and create role, but regardless, I get the same error. I'm on Windows with (PostgreSQL) 9.6.2
What am I doing wrong?
Update:
I tried using double quotes, but for some reason, postgres doesn't seem to like my double quotes. Using double quotes inside of single quotes mysteriously works -> Thanks #Laurenz
psql -U postgres -c "createuser my_app with createdb login password 'my_password';"
ERROR: syntax error at or near "createuser"
LINE 1: createuser my_app with createdb login password 'my_password'...

createuser is not an SQL command, so that won't work.
CREATE USER is correct, but you can't nest single quotes in single quotes like that.
It should be
psql -U postgres -c "CREATE USER my_app CREATEDB PASSWORD 'my_password'"

Related

vagrant postgreSQL run COPY

I want to COPY rows from a CSV file (From my Mac locally) into my postgreSQL DB, which is running on Vagrant.
As far as I know the COPY command can only be runned as super user.
This would be the command which fits:
COPY part_model FROM '/Users/myusername/Desktop/csv-file-to-import.csv' csv header;
Then running from my Terminal in the project folder I also have my Vagrantfile and I usually create the vagrant machine, I tried to run this command:
vagrant ssh -c "sudo su - postgres -c 'psql -d databasename name -U username -h localhost COPY part_model FROM '/Users/myusername/Desktop/part_model-import.csv' csv header'";
But I get this error/warning:
psql: warning: extra command-line argument "COPY" ignored
psql: warning: extra command-line argument "part_model" ignored
psql: warning: extra command-line argument "FROM" ignored
psql: warning: extra command-line argument "/Users/antonhorl3/Desktop/part_model- Import.csv" ignored
psql: warning: extra command-line argument "csv" ignored
psql: warning: extra command-line argument "header" ignored
Any help appreciated! Thanks!

executing query from docker to psql db shows psql: warning: extra command-line argument "" ignored

Hi I am in my docker console and trying to run shell script to connect to PSQL DB and execute queries.
#!/bin/sh
query = "select * from default$default."LightZone"";
#psql -U postgres -p postgres psql -e "$query";
This is my tablecreation.sh file and when I do ./tablecreation.sh
./tablecreation.sh: 5: ./tablecreation.sh: query: not found
psql: warning: extra command-line argument "" ignored
Password for user prisma:
psql (10.3 (Debian 10.3-1.pgdg90+1))
Type "help" for help.
The above message comes . But the same query is running from the psql command prompt.
can someone help me if i am missing something . I want to connect to postgres->prisma db >default$default schema > LightZone table and run some queries.
Escape the double quotes with \
query = "select * from default$default.\"LightZone\"";

psql: FATAL: role "user" does not exist

My psql does not open. Weirdly enough. I've created a user "Ben" through logging using psql -U postgres that lets me open PSQL and I run a command
CREATE USER Ben WITH PASSWORD '123';
After I do that it says ERROR: role "ben" already exists
But when I run psql it throws me psql: FATAL: role "Ben" does not exist
When you issue an SQL command like this:
CREATE USER Ben WITH PASSWORD '123';
identifiers within this command (such as the user name) are folded to lower case. So the user is actually created as 'ben'.
When you issue a shell command such as:
psql -U Ben mydatabase
then the identifier case is preserved, making it case sensitive.
If you really want the user name capitalized, then double quote it in SQL, like this:
CREATE USER "Ben" WITH PASSWORD '123';
Otherwise leave it all lower case and connect with:
psql -U ben mydatabase
See: Identifiers & Keywords in the manual

Entering a .sql file into a new postgreSQL database

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.

Unable To Run PSQL

Whenever I try to run 'psql' I receieve the following error message:
ahcarpenter#ubuntu:~$ psql
psql: FATAL: role "ahcarpenter" does not exist
Any ideas on how to fix this?
That happens because
$ psql
is equivalent to
$ psql ahcarpenter -U ahcarpenter
As that user does not exist enter as user postgres
$ psql -U postgres
Once inside create the user and the database "ahcarpenter".
create user ahcarpenter;
create database ahcarpenter with owner ahcarpenter;
exit
\q
Reenter
$ psql