英文:
How can I put an sql file into a postgresql database with a command line using URI?
问题
I've wrote an sql file with some inserts I want to do in my database.
Here's an example of what it's made of :
-- User
INSERT INTO users (user_id, credit, email, passwd, alias_login, user_id)
VALUES (1, 250, 'test@test.com', '123456', '1111', 'admin');
And I've also wrote a batch script which install my database before and I want after the installation to put some datas inside it with this sql script inside the batch script.
I've already explore all the options and to be fully automatised I need to connect to my db with a string to avoid password typing like that :
C:\PROGRA~1\PostgreSQL.2\bin\psql.exe "postgres://mydbadmin:mydbadminpasswd@localhost:5432/mydb"
But the thing that I want is to input to this my sql script considering that with this string method I can't use the -f option or any -c "\i ..." thing. It just ignores the command.
Also, I've tried this which didn't work at all :
type %cd%/myscript.sql | C:\PROGRA~1\PostgreSQL.2\bin\psql.exe "postgres://mydbadmin:mydbadminpasswd@localhost:5432/mydb"
英文:
I've wrote an sql file with some inserts I want to do in my database.
Here's an example of what it's made of :
-- User
INSERT INTO users (user_id, credit, email, passwd, alias_login, user_id)
VALUES (1, 250, 'test@test.com', '123456', '1111', 'admin');
And I've also wrote a batch script which install my database before and I want after the installation to put some datas inside it with this sql script inside the batch script.
I've already explore all the options and to be fully automatised I need to connect to my db with a string to avoid password typing like that :
C:\PROGRA~1\PostgreSQL.2\bin\psql.exe "postgres://mydbadmin:mydbadminpasswd@localhost:5432/mydb"
But the thing that I want is to input to this my sql script considering that with this string method I can't use the -f option or any -c "\i ..." thing. It just ignores the command.
Also, I've tried this which didn't work at all :
type %cd%/myscript.sql | C:\PROGRA~1\PostgreSQL.2\bin\psql.exe "postgres://mydbadmin:mydbadminpasswd@localhost:5432/mydb"
答案1
得分: 1
psql -d "postgresql://user:password@host/dbname" -c "SELECT 42"
?column?
══════════
42
(1 row)
英文:
<code>psql -c <command></code> works just fine with a connection string URL:
psql -d "postgresql://user:password@host/dbname" -c "SELECT 42"
?column?
══════════
42
(1 row)
答案2
得分: 0
你可以使用PGPASSWORD
环境变量或者使用.pgpass
文件(在Windows上是pgpass.conf
)。
使用PGPASSWORD
- 在Linux上,你可以这样使用:
PGPASSWORD=mypassword psql -U myuser -f myscript.sql mydb
注意:你可能需要采取措施,以防它留在你的Bash历史记录中。
- 在Windows上(假设是
cmd.exe
):
SET PGPASSWORD=mypassword
C:\PROGRA~1\PostgreSQL5.2\bin\psql.exe -U myuser -f myscript.sql mydb
使用pgpass
在Linux上,该文件应该是$HOME/.pgpass
。在Windows上,它是%APPDATA%\postgresql\pgpass.conf
。
这是一个以纯文本格式的文件:
hostname:port:database:username:password
例如:
localhost:5432:mydb:myuser:mypassword
英文:
You could either use the PGPASSWORD
environment variable or use a .pgpass
file (pgpass.conf
on Windows).
Using PGPASSWORD
- On Linux, you could just use:
PGPASSWORD=mypassword psql -U myuser -f myscript.sql mydb
Note: you may want to take measures so that it doesn't stay in your Bash history.
- On Windows (assuming
cmd.exe
):
SET PGPASSWORD=mypassword
C:\PROGRA~1\PostgreSQL5.2\bin\psql.exe -U myuser -f myscript.sql mydb
Using pgpass
On Linux, the file should be $HOME/.pgpass
. On Windows, it's %APPDATA%\postgresql\pgpass.conf
.
This is a plain text file with this format:
hostname:port:database:username:password
For example:
localhost:5432:mydb:myuser:mypassword
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论