Passing .sql file to Postgresql psql command - postgresql

I am trying to run the following file.sql using psql command (Postgresql v8.4):
CREATE OR REPLACE VIEW my_view AS
SELECT * FROM MY_ORDER_TABLE;
This is the command that I use:
psql -d myDB -f file.sql
But I get the following error:
psql:file.sql:1: ERROR: syntax error at or near "CREATE"
LINE 1: CREATE OR REPLACE VIEW my_view AS
Any suggestions on how to fix this?

Related

Syntax error in "psql" , command not get executed

I am using timescaledb.
The doumentation I am following is Using PostgreSQL's COPY to migrate data from a csv file to timescale db. The name of csv file is test.csv.
I created the db named test , the name of table is test1. Table is a hypertable as per the timescaledb documentation.
The table's structure and csv files structure are the same.
While executing the following command in cmd I am not getting a result other than an addition of - symbol in the console command test-#
psql -d test -c "\COPY test1 FROM C:\Users\DEGEJOS\Downloads\test.csv CSV"
If I put ; after the command
psql -d test -c "\COPY test1 FROM C:\Users\DEGEJOS\Downloads\test.csv CSV"; I am getting a syntax error at Line 1.
How can I solve this error and insert data from csv file to db.?
You are trying to run psql with \COPY inside psql session, thus you get an error in the second call, since psql keyword does not exist in SQL. psql is an executable.
To follow the instructions from Timescale, you need to call the command directly in CMD. I.e, call:
psql -d test -c "\COPY test1 FROM C:\Users\DEGEJOS\Downloads\test.csv CSV"
If you are in C:\Users\DEGEJOS as in your screenshoot, it will look like:
C:\Users\DEGEJOS\psql -d test -c "\COPY test1 FROM C:\Users\DEGEJOS\Downloads\test.csv CSV"

PostgreSQL pg_dump syntax error in Ubuntu

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`

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

postgres: how to execute script inside a .sql file

In SQLite you can do
sqlite3 i.db < x.sql
where x.sql is a create table statement and i.db is the database
What is the equivalent in PostgreSQL ?
The default command line tool for Postgres is psql it supports command line parameters to specify the database and a script name:
psql -d db_name -f x.sql
For details (e.g. how to specify the database user you want to use for the connection) see the manual: https://www.postgresql.org/docs/current/static/app-psql.html
With a similar approach:
psql -U your_role your_db < x.sql
Within psql:
\i /path/to/x.sql

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.