vagrant postgreSQL run COPY - postgresql

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!

Related

psql faild to restor file from network drive

i have a dump file on drive z (network drive)
im opening the psql from PgAdmin4
this is the command that im writeing:
psql -U postgres -d postgres -f Z:\DB_BU\md_20220729.sql
and this is the error that im getting:
Invalid command \DB_BU. Try \? for help.
when im doing this:
psql -U postgres -d postgres -f i\ Z:\DB_BU\md_20220729.sql
Invalid command \DB_BU. Try ? for help.
and when im doing this:
psql -U postgres -d postgres -f "Z:\DB_BU\md_20220729.sql"
im not getting any error but also its not restoring the file. how can i restor the file?
You're trying to call psql from within psql or PGAdmin. Since
psql is a standalone program, not an SQL command you can run in PGAdmin SQL window or psql's own, internal meta-command you're getting the error
Invalid command \DB_BU. Try \? for help
indicating that there was an attempt to interpret your entire command as a SQL query or an internal command and that this attempt failed.
You can open "psql tool" from within PGAdmin but your command won't work there either because it's trying to call psql itself, with some command-line options, which you cannot do when you're already inside an interactive psql session. The command
psql -U postgres -d postgres -f Z:\DB_BU\md_20220729.sql
can be used outside psql and PGAdmin, in your terminal, like zsh on Mac, sh/bash on Linux, cmd or PowerShell on Windows, where psql is installed and visible, along with your network path.
If you're able to open the psql tool window in PGAdmin, you can instead try and use an internal psql \i meta-command which is basically the same thing as the -f command-line option, but meant for use inside the psql session:
\i "Z:\DB_BU\md_20220729.sql"

Postgres: Create role in one line

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'"

Postgres: \copy syntax

With PostgreSQL 9.5 on CentOS 7, I have created a database named sample along with several tables. I have .csv data in /home/MyUser/data for each table.
For example, there exists TableName.csv for the table "TableName".
How do I load the csv files into each table?
What I've tried doesn't work and I can't figure out what I'm doing wrong.
Load from within the DB
$ psql sample
sample=# COPY "TableName" FROM '/home/MyUser/data/TableName.csv' WITH CSV;
ERROR: could not open file "/home/MyUser/data/TableName.csv" for reading: Permission denied
This implies a file permission problem. All the files in data/ are -rw-r--r-- and the directory itself is drwxr-xr-x. So file permissions shouldn't be the problem (unless I'm missing something). The internet says that COPY has problems with permissions and to try \copy.
Load from CLI
$ psql \copy sample FROM /home/MyUser/data/TableName.csv WITH CSV
psql: warning: extra command-line argument "FROM" ignored
psql: warning: extra command-line argument "/home/MyUser/data/TableName.csv" ignored
psql: warning: extra command-line argument "WITH" ignored
psql: warning: extra command-line argument "CSV" ignored
psql: FATAL: Peer authentication failed for user "sample"
This appears to be a syntax error, but I'm not finding the documentation particularly helpful (man psql then /\copy). I've also tried the following to the same result.
$ psql \copy sample."TableName" FROM /home/MyUser/data/TableName.csv WITH CSV
$ psql \copy sample FROM /home/MyUser/data/TableName.csv WITH DELIMITER ','
There are several other permutations which yield similar errors.
Web Resources Used
https://www.postgresql.org/docs/9.5/static/app-psql.html
https://www.postgresql.org/docs/9.5/static/sql-copy.html
The correct COPY command to load postgreSQL data from csv file that has single-quoted data?
https://soleil4716.wordpress.com/2010/08/19/using-copy-command-in-postgresql/
Can I use \copy command into a function of postgresql?
https://wiki.postgresql.org/wiki/COPY
About the permissions:
Don't forget that to access a file you need permissions on all directories in the path. So if, for example, the OS user postgres does not have permissions on the /home/MyUser directory, you get the observed error message.
About \copy:
You have to use the -c option to supply a command to psql:
$ psql -c "\copy sample FROM '/home/MyUser/data/TableName.csv' WITH (FORMAT CSV)"
Adding, this sample was helpful from Laurenz, as I got this to work first time today from a windows 10 client.
My target is an enterprise greenplum db (same thing just parallel/scale).
Step 1, Postgres client installed (I didn't install the database itself, but all the other items as part of the windows bundling install, including the shell most importantly for this)
https://www.postgresql.org/download/
Step 2, Create your target table on your workspace/db. I use DBeaver to connect to greenplum db, then run this, you don't have to use DBeaver, you can probably run this in the shell itself.
CREATE TABLE workspacename.scott_test2 (
blah1 varchar(40) NOT NULL,
blah2 varchar(40) NOT NULL
)
DISTRIBUTED BY (blah1);
Step 3, launch the shell command from postgres utilities installed earlier, login as it prompts you, then when you're in, the command I ran in that shell installed above (a command line interface) is simply:
\copy workspacename.scott_test2 FROM 'C:\temp\flatfile.csv' WITH CSV;
Note \copy is not copy. Use \copy
This loaded a half million row table in 2 seconds, fast. file above is comma delimited, the with CSV does that.

psql extra command line argument

Can someone tell me why I get the error extra command line argument here? When I use -f and give it the full path to the sql file it works fine. I would like to use a relative path instead so I was trying to use the \ir command.
psql -c \c postgresql://docker:1234/nbt?ssl=true -U admin -v username='user73291' -v recipeId=2 -c \ir '../../../resources/sql/myfile.sql'
Error:
psql: warning: extra command-line argument "../../../resources/sql/myfile.sql" ignored
ERROR: syntax error at or near "ir"
LINE 1: ir
^
Thanks!
\i is a meta-command to be used in the psql command line, not in the shell command line. What is the problem with -f?

NSIS running pg_restore or psql commands - failing to restore DB properly

In an NSIS installer, I use the following line to restore a PostgreSQL database from a file packaged with the installer.
ExecWait '$pg_restore_path --host 127.0.0.1 --no-password --port 5432 --username "postgres" --dbname "myDatabase" --verbose $EXEDIR/myDatabase.backup' $0
The command works but pg_restore seems to incorrectly set certain postgres sequences' current values (the current values either get reset to 1 or a number lower than that of the source) resulting in collisions. It seems to be a native bug with postgres but really not sure.
I have also tried replacing pg_restore with psql like this:
ExecWait '$psql_path -f "$EXEDIR/myDatabase.sql" myDatabase'
Which does not work; the terminal pops open and closes. Or like this:
ExecWait '$psql_path myDatabase < $EXEDIR/myDatabase.sql'
Which causes the error psql: warning: extra command-line argument "<" ignored
When I run the psql command manually from the command line it works like a charm, and sequences set properly. So my question is how to get the psql command working in NSIS with the file feeded < and avoiding the error. Failing that, any insight on using pg_restore differently that could work around the sequence issue?
Thanks
The following NSIS commands seem to have solved it.
ExecWait '$createdb_path -h "127.0.0.1" -p "5432" -U "postgres" -T "template1" --owner "user_owner" myDatabase'
ExecWait '$psql_path -f "$INSTDIR/myDatabase.sql" myDatabase user_owner'