PostgreSQL pg_dump syntax error in Ubuntu - postgresql

In ubuntu 16.04 when i want to run pg_dump it doesn't work i got this error syntax error. What is wrong ?
postgres=# pg_dump db_name > db_name1.sql
postgres-#
postgres-# ;
ERROR: syntax error at or near "pg_dump"
LINE 1: pg_dump db_name > db_name1.sql
^
postgres=#

pg_dump is not an SQL command.
It is a stand-alone utility, so you cannot run it from SQL query.
Follow the link for more information.
For your case: type \q<Enter> to quit from SQL-client and repeat your command again using shell prompt.
Remember you should use your SQL-credentials.
I.e.:
$ pg_dump -U <postgres_user_name> db_name > db_name1.sql`

Related

PostgreSQL - using psql terminal to restore a dump - error: stdin is not a tty

On Windows 10, using psql with PostgreSQL 14 installed, I'm trying to restore a dump that is my_dump.sql.
I'm on Bash terminal currently.
I'm connected to psql server and I created a new database new_db, then a new superuser tobe.
Then I tried the command psql -d new_db < my_dump.sql but I'm getting the error stdin is not a tty
I tried the solution psql -U tobe -d new_db -f my_dump.sql from "stdin is not a tty" when populating Postgres database
because they mention the error but now I get a new error:
ERROR: syntax error at or near "ÿþ" LIGNE 1 : ÿþ
Are the two errors connected? Maybe the second error is only related to the command syntax. I'm not sure if I'm in the right direction.
Try
psql.exe -U tobe -d new_db -f my_dump.sql

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\"";

How to add Postgres extensions via .sh file

I'm trying to add extensions for Postgres by writing the following script (setup.sh):
sudo -u postgres psql my_database
CREATE EXTENSION adminpack;
when I do vagrant up, it supposed to run and add extensions automatically by running the script. However, I got the error message that
==> default: ERROR: syntax error at or near "exit"
==> default: LINE 1: exit
==> default: ^
==> default: /tmp/vagrant-shell: line 24: CREATE: command not found
Please note that I have installed all the necessary postgres stuff to run the code above. In addition, when I enter these command manually, it successfully creates the extension. Thanks to whoever that helps.
try:
sudo -u postgres psql my_database -c "CREATE EXTENSION adminpack"
https://www.postgresql.org/docs/current/static/app-psql.html
-c command
--command=command
Specifies that psql is to execute the given command string, command.
also consider using -f sql_file.sql for more complicated scripts, or smth like:
psql <<EOF
\x
SELECT NOW();
SELECT * FROM foo;
EOF

shp2pgsql command not working

I am new to PostgreSQL and am trying to import a shapefile via the terminal with the following code:
shp2pgsql -I -s 4269 C:\MyData\roads\roads.shp roads | psql -U postgres -d <DBNAME>
The postgis extension has already been created. But I continue to get a syntax error:
postgres=# CREATE EXTENSION postgis;
ERROR: extension "postgis" already exists
postgres=#
postgres=# shp2pgsql -I -s 4326 /Users/alexander/Downloads/pluto/pluto.shp pluto | psql -U postgres -d postgres;
ERROR: syntax error at or near "shp2pgsql"
LINE 1: shp2pgsql -I -s 4326 /Users/alexander/Downloads/pluto/pluto....
^
postgres=#
Any idea as to what the problem could be?
Well that's because shp2pgsql is a command line executable. Something that you run in your shell (bash,sh etc). It's not intended to be executed in your psql console as you seem to be doing.

altering privilege in psql interactive prompt

I am new to the use of psql prompt. I ran sudo -u postgres (psql version 9.3.9)
Then I connected to my database and I ran
=> ALTER DEFAULT PRIVILEGES FOR USER some_user IN SCHEMA public GRANT SELECT ON TABLES TO other_user;
and then I got this error message
ERROR: syntax error at or near "ALTER DEFAULT PRIVILEGES FOR USER"
LINE 1: ALTER DEFAULT PRIVILEGES FOR USER ckan_default IN SCHEMA pub..
How can I deal with that ?