I'm going around in circles and so need some help.
I want to be able to run an .sql against a database which is a scheduled task via a bat file. However when i run the bat file manually I get prompted for a password. I enter the password and then get told that....
"psql: FATAL: password authentication failed for user "(my windows login)"
'-h' is not recognised as an internal or external command,
operable program or batch file.
At the moment my bat reads...
#echo off
"D:\Program Files (x86)\PostgreSQL\9.1\bin\psql.exe"
-h localhost -U postgres -d database_name -f D:/scripts/SQL/test.sql
pause
First thing, what cmd do i need to add to populate the password request
What am I doing wrong with the rest of the statement to get it to load the .sql
Thanks
by adding this line to your config file (pg_hba.conf), you can tell postgres to allow local connections without authentication
local <database> <user> trust
http://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html
All that needs to go into a single line in your batch file (it seems you have two lines):
#echo off
"D:\Program Files (x86)\PostgreSQL\9.1\bin\psql.exe" -h localhost -U postgres -d database_name -f D:/scripts/SQL/test.sql
pause
To avoid the password prompt, set the environment variable PGPASSWORD before calling psql:
#echo off
setlocal
set PGPASSWORD=my_very_secret_password
"D:\Program Files (x86)\PostgreSQL\9.1\bin\psql.exe" -h localhost -U postgres -d database_name -f D:/scripts/SQL/test.sql
pause
endlocal
The setlocal/endlocal commands are there to make sure the variable is cleared after the batch file was executed.
To avoid having the password in plain text in the batch file you can create a pgpass.conf file that contains the password. For details please see the manual: http://www.postgresql.org/docs/current/static/libpq-pgpass.html
Related
I'm dealing with psql for the first time and I need a command that executes a .sql file and after executing, it should make a .log file with the script output. I'm looking for something similar to this other command that I use with SQL Server:
sqlcmd -U userid -P password -S serveraddress -i path_to_the_sql_file -o path_where_to_save_log_file
Can you help me, please? :-)
I would spend some time at the psql page.
A quick example:
psql -U userid -h serveraddress -d some_db -f path_to_the_sql_file -L=path_where_to_save_log_file
With Postgres you need to connect to a database with a client. There are other options for inputting commands and capturing output at the link above.
I want to export my database as a .sql file.
Can someone help me? The solutions I have found don't work.
A detailed description please.
On Windows 7.
pg_dump defaults to plain SQL export. both data and structure.
open command prompt and
run pg_dump -U username -h localhost databasename >> sqlfile.sql
Above command is preferable as most of the times there will be an error which will be something like - ...FATAL: Peer authentication failed for user ...
In windows, first, make sure the path is added in environment variables PATH
C:\Program Files\PostgreSQL\12\bin
After a successful path adding restart cmd and type command
pg_dump -U username -p portnumber -d dbname -W -f location
this command will export both schema and data
for only schema use -s in place of -W
and for only data use -a.
replace each variable like username, portnumber, dbname and location according to your situation
everything is case sensitive, make sure you insert everything correctly,
and to import
psql -h hostname -p port_number -U username -f your_file.sql databasename
make sure your db is created or creation query is present in .sql file
Documentation: https://www.postgresql.org/docs/current/app-pgdump.html
Go to your command line and run
pg_dump -U userName -h localhost -d databaseName > ~/Desktop/cmsdump.sql
I am trying to set up the step with Batch file path on particular time in pgAgent via pgAdmin. But when I run that it is failing and in Step statistics I got this Output
C:\Windows\system32>C:\postgresql\run.bat
'psql' is not recognized as an internal or external command,
operable program or batch file.
Details:
Postgresql 9.3.5 on local system account (Current User)
pgAdmin 1.18.1
pgAgent via stack builder with Administrator account (Current User)
in run.bat I have only two statement
#echo off
psql -h localhost -p 5433 -U postgres -d test -a -f "test.sql"
I have psql in system path variable and able to access it in cmd. When I run that bat file manually it is executing without fail. But when I given the batch file path (C:\postgresql\run.bat) in pgAgent jobs it is giving that error in statistics.
Is there anything wrong in my configuration? Why it is always going to that C:\Windows\system32>?
Edit:
My run.bat file
#ECHO OFF
SET LBSDatabaseName=Test
SET dbHost=localhost
SET dbPort=5434
SET dbUser=postgres
SET logFile=DbInstall.log
SET sqlFolder="D:\SOURCECODE\archivescripts"
"C:\Program Files (x86)\PostgreSQL\9.3\bin\psql.exe" -h "%dbHost%" -p "%dbPort%" -d "%LBSDatabaseName%" -U "%dbUser%" -L "%logFile%" -q -f "%sqlFolder%\Archive.sql"
My Archive.sql
update "Archive".emp set "FirstName"='Srikanth Dyapa';
For example,
D:\pgAgent_jobs
is the path where psql located.
D:\pgAgent_jobs\scripts\test.sql
is the path in which my test.sql placed.
D:\pgAgent_jobs\scripts\psqlss.bat
is my bat file to execute test.sql
so my bat file will be like below
#echo off
cd /D D:\\pgAgent_jobs
psql -h localhost -p 5432 -U postgres -d db_name -a -f "D:\pgAgent_jobs\scripts\test.sql"
Note : my pg_hba.conf is configured with trust for all hosts that's why am not passing any password in the above psql command
DB: PostgreSQL 9.0
Client: Windows 7
Server Windows 2008, 64bit
I'm trying to connect remotely to a PostgreSQL instance for purposes of performing a pg_dump to my local machine.
Everything works from my client machine, except that I need to provide a password at the password prompt, and I'd ultimately like to batch this with a script.
I've followed the instructions here:
http://www.postgresql.org/docs/current/static/libpq-pgpass.html
But it's not working.
To recap, I've created a file on the server: C:/Users/postgres/AppData/postgresql/pgpass.conf, where PostgreSQL is the db user.
The file has one line with the following data:
\*:5432:\*postgres:[mypassword]
I've also tried replacing each * with [localhost|myip] and [mydatabasename] respectively.
From my client machine, I connect using:
pg_dump -h <myip> -U postgres -w [mydbname] > [mylocaldumpfile]
I'm presuming that I need to provide the -w switch in order to ignore password prompt, at which point it should look in the AppData directory on the server.
It just comes back with:
connection to database failed: fe_sendauth: no password supplied.
As a hack workaround, if there was a way I could tell the Windows batch file on my client machine to inject the password at the PostgreSQL prompt, that would work as well.
It works for me:
Use command line
cd %appdata%
mkdir postgresql
cd postgresql
notepad pgpass.conf
inside pgpass.conf paste your connection string (*:5432:*postgres:[mypassword]) and save the file.
To connect to postgres use:
psql/pg_dump -U <username> -h <host> -w <other params you want to use>
I have solved similar problem (only in Linux) to use ip address in pgpass and psql.
.pgpass
127.0.0.1:5432:db:dbuser:123
psql params
psql -d db -U dbuser -h 127.0.0.1 -w
pg_hba conf with default settings:
# IPv4 local connections:
84 host all all 127.0.0.1/32 md5
Create pgpass.conf file
Windows > Start > Run
type %APPDATA%
press OK
Create a folder: postgresql
Create a file : pgpass.conf (under postgresql folder)
Open pgpass.conf file
Now you should have below file ready, open it via below (making sure it exists):
Windows > Start > Run
type %APPDATA%\postgresql\pgpass.conf
press OK
Paste pgpass.conf file contents
Paste the below
# serverDomainOrIP:PORT:databaseName:userName:password
# 127.0.0.1:5432:myDbName:postgres:myPassword
# *:5432:*:*:myPassword
You can do one of the below:
- Remove # for the 3rd line, and give your password in the place of "myPassword"
OR
- Remove # for the 2nd line, and give ip (or, yourDomain.com), dbname, username & password
Hope that helps.
I've had a similar problem which I didn't manage to resolve - I couldn't get the script to recognise the pgpass.conf file. I however used a work-around of setting the PGPASSWORD environment variable in the batch file I was using (PostgreSQL 9.6).
Inside a batch file:
SET PGPASSWORD=<<password>>
pg_dump.exe -h <<host>> -p <<port>> -U <<user>> -Fc -b -v -f <<output file path>> <<database>>
I have gotten it to work with the following:
pgpass.conf:
127.0.0.1:5432:*:username:password
However, I have it stored here:
C:\Users\<user>\AppData\Roaming\postgresql
For some reason, on a previous iteration of Postgres, the database had generated the pgpass file and stored it there. I was running into the same issue you were having, moved it to that directory and it worked. I'm not sure why though.
Then, all you'll need to do is:
pg_dump -h myip mydb > mylocaldumpfile
...ensuring that myip and the ip in pgpass.conf are identical. If they are not, it will prompt you for a password.
You could use pgAdmin III to store the password (in the server's properties).
That operation automatically creates a correct pgpass.conf file. You can then schedule a task to run a simple batch file that would read:
"C:\path\to\pg_dump.exe" -U <user> -w <database> > C:\path\to\database.backup
Make sure you are logged in as the user corresponding with the folder where the pgpass.conf file lives.
If you are using UTF-8 encoding, please ensure that you are using without BOM mode.
Otherwise leave the first line as a comment:
# This line may contain hidden BOM bytes
localhost:5432:database:username:password
Also you don't need to escape asterisks \*, just put * to enable wildcard matching.
I am using PostgreSQL 8.4, and I have some *.sql files to import into a database. How can I do so?
From the command line:
psql -f 1.sql
psql -f 2.sql
From the psql prompt:
\i 1.sql
\i 2.sql
Note that you may need to import the files in a specific order (for example: data definition before data manipulation). If you've got bash shell (GNU/Linux, Mac OS X, Cygwin) and the files may be imported in the alphabetical order, you may use this command:
for f in *.sql ; do psql -f $f ; done
Here's the documentation of the psql application (thanks, Frank): http://www.postgresql.org/docs/current/static/app-psql.html
in command line first reach the directory where psql is present then write commands like this:
psql [database name] [username]
and then press enter psql asks for password give the user password:
then write
> \i [full path and file name with extension]
then press enter insertion done.
Well, the shortest way I know of, is following:
psql -U {user_name} -d {database_name} -f {file_path} -h {host_name}
database_name: Which database should you insert your file data in.
file_path: Absolute path to the file through which you want to perform the importing.
host_name: The name of the host. For development purposes, it is mostly localhost.
Upon entering this command in console, you will be prompted to enter your password.
Be careful with "/" and "\". Even on Windows the command should be in the form:
\i c:/1.sql
Always preferred using a connection service file (lookup/google 'psql connection service file')
Then simply:
psql service={yourservicename} < {myfile.sql}
Where yourservicename is a section name from the service file.
enter image description here
use following command :-
C:\Program Files\PostgreSQL\12\bin>psql -U username -d databasename -f D:\file.sql