简体   繁体   中英

How do I run an SQL file in PostgreSQL using a Linux terminal?

I am working as a software tester. I want to to test my project locally. So I want to insert the database using the psql command. I tried so many times. I couldn't get to access the login page.

This is what I did:

postgres@naveen-Inspiron-3542:/home/naveen$ psql -U admin docmgr

Password for user admin:  psql (9.5.4) Type "help" for help.

docmgr=> psql docmgr -f /var/www/html/docmgr/application/assets/dd/structs/docmgr_21Oct2016.sql -U admin
docmgr-> \dt No relations found.

docmgr-> \dt No relations found.

docmgr-> select * from admin_users;

ERROR:  syntax error at or
 near "psql" LINE 1: psql docmgr -f
 /var/www/html/docmgr/application/assets/dd/st...
         ^

This is wrong:

 docmgr=> psql docmgr -f /var/www/html/docmgr/application/assets/dd/structs/docmgr_21Oct2016.sql 

You are running psql from within psql but "psql" is not a SQL statement.

You either need to run directly from the command prompt:

postgres@naveen-Inspiron-3542:/home/naveen$ psql -U admin -d docmgr -f /var/www/html/docmgr/application/assets/dd/structs/docmgr_21Oct2016.sql

Or from within psql you need to use the \\i command to run a SQL script:

postgres@naveen-Inspiron-3542:/home/naveen$ psql -U admin docmgr

Password for user admin:  psql (9.5.4) Type "help" for help.

docmgr=> \i /var/www/html/docmgr/application/assets/dd/structs/docmgr_21Oct2016.sql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM