Postgres Vacuumdb sql - postgresql

Im using my localhost to try and vacuumdb from java using create statement. I try to run the psql syntax in linux command line to verify if the syntax is indeed correct:
wsemp=# vacuumdb -d wsemp -z -v -h localhost -U jboss;
ERROR: syntax error at or near "vacuumdb"
LINE 1: vacuumdb -d wsemp -z -v -h localhost -U jboss;
I tried almost everything by removing some options and changing it to this:
wsemp=# vacuumdb --host=127.0.0.1 --port=5432 --dbname=wsemp --username=dbauser --analyze --verbose;
but the same error shows up. Any idea as to why?

vacuumdb is the command-line tool. The sql command is VACUUM. The syntax for options is a bit different: this is the documentation for it.
I'm guessing from the arguments you pass to vacuumdb that you'd want something like:
wsemp=# VACUUM (VERBOSE, ANALYZE);

Related

Why does psql only works when I pass in a string?

This fails with tlsv1 alert unknown ca
psql -h localhost -p 4566 -d dev -U root --set=sslmode=disable
This works:
psql "port=4566 host=localhost user=root dbname=dev sslmode=disable"
Why? Why does one work when the other does not? Is the --set ignored?
Is this a bug or a feature?
The --set is not ignored, it just doesn't do anything meaningful. It tells psql to set the psql variable named 'sslmode', but that variable is not in charge of anything. If you could connect and you then ran select :'sslmode';, you find that it had indeed been set, but since it isn't in charge of anything this doesn't really matter much.
TheA way to do this correctly, assuming you are using bash, is:
PGSSLMODE=disable psql -h localhost -p 4566 -d dev -U root
https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
The second one works. Because (sslmode=disable) is part of the connection string key words.
psql --help returns:
-s, --single-step single-step mode (confirm each query)
-S, --single-line single-line mode (end of line terminates SQL command)
also if you psql --help | grep ssl, there is zero result. which mean you cannot use simple use
psql -h localhost -p 4566 -d dev -U root --sslmode=disable .
jjanes's answer works because: https://www.postgresql.org/docs/current/libpq-envars.html

postgres script silently pass without any result

i am trying to execute psql queries from the bash command line passing password in following format
set PGPASSWORD=rtttttul psql -U ostgres -h localhost -d postgres -c "select * from logs" -o output.txt
Somehow my queries are not giving any results.i have tried to pass different queries or incorrect credentials but still script execute without any error.
If i don't pass password and try logging in to command prompt,everything works fine.
i want to check what basic thing i am missing above
Below command worked
PGPASSWORD=rtttttul psql -U ostgres -h localhost -d postgres -c "select * from logs" -o output.txt
remove set at start of command fixed it

pg_restore not working for .dump file

I'm trying to restore a database to a new environment and pg_dump/pg_restore seems like the best way for me to do that.
I've run the pg_dump command with the following code which has produced my dump file:
pg_dump -v -Fc --host=test.postgres.database.azure.com --port=5432 --username=test#test --dbname=test > test.dump
However when I run pg_restore it is not able to restore the database. I have run into two separate errors when trying two separate commands. The first error
occurs when i use the following command
pg_restore -h test.postgres.database.azure.com -p 5432 -U test#test -C test.dump
and the second
occurs upon using this command
pg_restore -h test.postgres.database.azure.com -p 5432 -U test#test -C -Fc test.dump
I really don't understand what is going wrong here. All other answers I have found that get these same errors were encountered when people tried to restore plain text files but that's not what I'm attempting to do here. Any help would be greatly appreciated.

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'

Backing Up mysql db from the command line

I am trying to backup my database and keep getting error :
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$ mysqldump -u root -p chandlers > backup_db.sql' at line 1
I am using the following command to launch the backup :
$ mysqldump -h localhost -u root -p chandlers > backup_db.sql
edit>>>>
This is how I connect to the db, this is a shortcut I have setup to open a command line :
C:\server2go\server2go\server\mysql\bin\mysql.exe -h localhost -P 7188 -u root
This works fine and connects, I have tried creating another like this >>
C:\server2go\server2go\server\mysql\bin\mysqldump.exe $ mysqldump -h localhost -P 7188 -u root -pchandlers > backup_db.sql
but I am getting an access denied error now.
you can use mysqldump to backup mysql database.
Below is the script example to backup mysql database in command line:-
$ mysqldump -h localhost -u username -p database_name > backup_db.sql
If your mysql database is very big, you might want to compress your sql file.
Just use the mysql backup command below and pipe the output to gzip,
then you will get the output as gzip file.
$ mysqldump -u username -h localhost -p database_name | gzip -9 > backup_db.sql.gz
If you want to extract the .gz file, use the command below:-
$ gunzip backup_db.sql.gz
You seem to be calling mysqldump from within mySQL, which is incorrect - it's a separate executable.
Call it from the command line instead.
I think it should be $ mysqldump -h localhost -u root -pchandlers > backup_db.sql. Otherwise it seems fine to me.