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

血红的双手。 提交于 2019-12-24 06:50:02

问题


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...
         ^

回答1:


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


来源:https://stackoverflow.com/questions/40214589/how-do-i-run-an-sql-file-in-postgresql-using-a-linux-terminal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!