psql: FATAL: invalid value for parameter "client_encoding": "WIN1252" - postgresql

I am using psql with Redshift. Unfortunately, Redshift is uses PostgreSQL 8.0.2.
So when I downloaded the latest version of PostgreSQL (9.6.1) and try logging into a database, I get the error:
cannot connect from psql due to invalid parameter "client_encoding"
Instructions here https://forums.aws.amazon.com/thread.jspa?messageID=529120 suggest
set PGCLIENTENCODING=UTF8
But when I open SQL Shell (psql) in Windows, I am never given the chance to enter this command:
How can I bridge this gap/fix this error? Thanks

set x=y is the Windows CMD syntax to set an environment variable, just as x=y;export x or export x=y or setenv x y is on most Unix shells.
You can pre-set a (semi)permanent env var in Windows from ControlPanel / System (or ThisPC rightclick Properties), AdvancedSystemSettings, Advanced tab, EnvironmentVariables button. Or from CMD with the SETX command; SETX /? for help or http://ss64.com/nt/setx.html . (Or directly in the registry, but if you have enough skill to use regedit safely you wouldn't ask this Q.)

Related

Execute many db2 commands in one command

can I run multiple db2 commands in one command?
i.e: from cmd:
db2cmd /c db2 /c connect to sample user sample_user using sample_pwd /c
"SELECT * FROM table;"
I also tried the following:
db2 connect to sample user db2admin using pwd; EXPORT TO result.csv OF DEL
MODIFIED BY NOCHARDEL SELECT * FROM alarms;
but didn't work with the following error:
SQL0104N An unexpected token "EXPORT" was found following
"". Expected tokens may include: "NEW". SQLSTATE=42601
as an example, for VERTICA, vsql tool, this can be done this way:
vsql -h localhost -U user -w pwd -c "SELECT * FROM alarms" -A -o
"alarms.csv" -F "|" -P footer=off -q
You appear to be using Microsoft Windows db2cmd.exe .
Your question has nothing to do with Db2 per se, but it is instead more about CMD (cmd.exe) scripting syntax, a legacy scripting language for batch files by Microsoft that still works on Windows-10, and which also works in db2cmd.exe.
In a db2cmd.exe shell you can use the "&&" sequence between distinct Db2 commands (and each such command must have the db2 prefix). Additionally each such command line has to escape any of the characters that are special characters to the shell itself. By default the escape character is a caret (^) symbol.
For example db2 connect to dbname && db2 ^"export to alarms.csv of del ... select ^* from alarms^" && db2 connect reset
( I show the ^ before any " that you might want to pass to Db2-CLP ).
But that && will require that each command returns a zero exit code, which might not be what you want, although it is usually the safest option. If a previous command fails then subsequent commands will not run.
If you want to tolerate some non-zero exit codes, use bracketing ( ... ) to group commands, and then use the && or & outside the brackets depending on your requirements. You can read about CMD scripting in any good book, plenty of examples online.
However, when scripting for Db2 on Windows , it can be much wiser to append all of the commands (without the Db2 prefix) into a plain text file, and then ask the Db2 clp to execute the text file via the syntax db2 -tvf texfile. Doing it this way lets you add conditional logic inside the textfile, handle exceptions , avoid shell escaping requirements, etc. If you encapsulate all your logic inside a script, it makes it easier to test, and also easier to run from a single db2cmd /c .... command-line.
If you want to make a batch file (*.bat or *.cmd) that does not need the db2cmd prefix to be invoked, you can alter your batch file to have a few lines at the start of the batch file to re-execute itself via db2cmd.exe. This works better if your db2cmd.exe is already on the PATH environment variable, but if that is not the case then you can fully-qualify the absolute pathname to your db2cmd.exe inside the batch file. The lines to add at the start of the batch file are:
#rem re-execute via db2cmd if running from cmd.exe
#echo off
if "%DB2CLP%"=="" db2cmd /c /i /w "%0" %* & goto :EOF
db2 connect to sample user db2admin using pwd
if errorlevel 1 #echo "Failed to connect to database " && #goto :EOF
db2 "EXPORT TO result.csv OF DEL MODIFIED BY NOCHARDEL SELECT * FROM alarms"
if errorlevel 3 #echo "Export from Db2 failed" && #goto :EOF
Additionally on Windows, you can use Powershell scripting to maniuplate Db2 databases, and you can also use Windows subsystem for unix to run Unix-style shell scripts in some configuration.
Your example's most direct comparison in Db2-land would be clpplus which lets you specify the database but you also have to provide login information (including a password or you can be prompted for it).
Within the db2cmd and db2 framework you have a couple options but most likely will want to use a script file.
One option: Set the registry variable DB2DBDFT to your default database. Personally, I dislike this option because it causes an implicit connection to a database that you may not have intended.
One option: Put your series of commands into a file and run that file. This is the more traditional way of running multiple commands. Commands can be terminated with a semi-colon and newline (it understands DOS and Unix differences here). You can use a different terminator by using -td # (for example). You would then invoke db2 -tf file.sql.
One option: A batch file. It's similar to above but you'd use the db2cmd environment to execute a batch that has the db2 commands in it. db2cmd gets you an appropriate environment for working with Db2. If you connect to a database in this environment you stay connected until you issue a CONNECT RESET, a TERMINATE, are forcibly disconnected, or your environment exits. So your batch file would simply have:
db2 connect to sample user db2admin using pwd
db2 "EXPORT TO result.csv OF DEL MODIFIED BY NOCHARDEL SELECT * FROM alarms"
(note the quotes to keep the command line from substituting all the filenames in the current working directory where the * is)
Two options.
1-st:
db2cmd /i /w /c "db2 ^"connect to sample^" & db2 ^"values 1^" & db2 connect reset"
2-nd:
You may set the following Windows system environment variable DB2CLP to the value **$$** and run db2 commands from windows cmd directly afterwards like this:
db2 "connect to sample" & db2 "values 1" & db2 connect reset

How to use pg_squeeze on Windows

I tried to make / cmake pg_squeeze on Windows but it failed.
C:\pg_squeeze-REL1_0_PG_10>cmake c:\pg_squeeze-REL1_0_PG_10 CMake
Error: The source directory "C:/pg_squeeze-REL1_0_PG_10" does not
appear to contain CMakeLists.txt. Specify --help for usage, or press
the help button on the CMake GUI.
Is there any possibility to use pg_squeeze on Windows?
Are there any other extensions for Postgresql 9.6 and above to work on Windows for routine database maintenance?
C:\pg_squeeze-REL1_0_PG_10>cmake c:\pg_squeeze-REL1_0_PG_10 CMake
Error: The source directory "C:/pg_squeeze-REL1_0_PG_10" does not
appear to contain CMakeLists.txt. Specify --help for usage, or press
the help button on the CMake GUI.
Are there any other extensions for Postgresql 9.6 and above to work on Windows for routine database maintenance?
Yes, there's pgcompacttable Here's how to make it work on Windows:
0.) Enter PostgreSQL's CLI
psql --username=postgres --dbname your_db
Enable extension pgstattuple for that table
CREATE EXTENSION pgstattuple;
1.) Install ActivePerl
2.) Open pgcompacttable in text editor and Find-Replace
getpwuid($<)
with
getlogin()
3.) Replace
my $user_login = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
with
my $db_user = '';
4.)
set /p PGPASSWORD="Enter password: "
set PGUSER=postgres
set PGHOST=127.0.0.1
set PGPORT=5432
%SystemDrive%\perl64\bin\perl.exe pgcompacttable --dbname *your_db* -f >> pgcompacttable.log

How can I start PostgreSQL on Windows?

I have installed Postgresql on my Windows 10 PC. I have used the pgAdmin II tool to create a database called company, and now I want to start the database server running. I cannot figure out how to do this.
I have run the start command on the postgres command line, and nothing seems to happen.
What I doing is:
postgres=# pg_ctl start
postgres=# pg_ctl status
postgres=# pg_ctl restart
postgres=# pg_ctl start company
postgres=# pg_ctl status
.....-> I am seeing nothing returned.
Go inside bin folder in C drive where Postgres is installed.
run following command in git bash or Command prompt:
pg_ctl.exe restart -D "<path upto data>"
Ex:
pg_ctl.exe restart -D "C:\Program Files\PostgreSQL\9.6\data"
Another way:
type "services.msc" in run popup(windows + R).
This will show all services running
Select Postgres service from list and click on start/stop/restart.
Thanks
pg_ctl is a command line (Windows) program not a SQL statement. You need to do that from a cmd.exe. Or use net start postgresql-9.5
If you have installed Postgres through the installer, you should start the Windows service instead of running pg_ctl manually, e.g. using:
net start postgresql-9.5
Note that the name of the service might be different in your installation. Another option is to start the service through the Windows control panel
I have used the pgAdmin II tool to create a database called company
Which means that Postgres is already running, so I don't understand why you think you need to do that again. Especially because the installer typically sets the service to start automatically when Windows is started.
The reason you are not seeing any result is that psql requires every SQL command to be terminated with ; in your case it's simply waiting for you to finish the statement.
See here for more details: In psql, why do some commands have no effect?
If you have installed postgres via the Windows installer you can start it in Services like so:
After a lot of search and tests i found the solution :
if you are in windows :
1 - first you must found the PG databases directory
execute the command as sql command in pgAdmin query tools
$ show data_directory;
result :
------------------------
- D:/PG_DATA/data -
------------------------
2 - go to the bin directory of postgres in my case it's located "c:/programms/postgresSql/bin"
and open a command prompt (CMD) and execute this command :
pg_ctl -D "D:\PSG_SQL\data" restart
This should do it.
The simplest way to start/stop/restart the installed PostgreSQL Server on your Windows device is as follows:
Start -> net start postgresql-x64-14
Stop -> net stop postgresql-x64-14
Restart -> net stop postgresql-x64-14 && net start postgresql-x64-14
The version number must be changed to take into account the installed version of your PostgreSQL Server.
For windows the following command worked well for me
pg_ctl.exe restart -D "<path_to_data>"
Eg: pg_ctl.exe restart -D "D:\Program Files\PostgreSQL\13\data"
If you are getting an error "psql.exe' is not recognized as an internal or external command,... "
There can be :
Causes
System is unable to find the psql.exe tool, because the path to this tool is not specified in the system environment variable PATH
or
- PostgreSQL Database client not installed on your PC
Since you have already installed PostgreSQL the latter can not be the issue(assuming everything is installed as expected)
In order to fix the first one "please specify the full path to the bin directory in the PostgreSQL installation folder, where this tool resides."
For example
Path: "C:\Program Files\PostgreSQL\10\bin"
I found using
net start postgres_service_name
the only reliable way to operate Postgres on Windows
first find your binaries file where it is saved.
get the path in terminal mine is
C:\Users\LENOVO\Documents\postgresql-9.5.21-1-windows-x64-binaries
(1)\pgsql\bin
then find your local user data path, it is in mostly
C:\usr\local\pgsql\data
now all we have to hit following command in the binary terminal path:
C:\Users\LENOVO\Documents\postgresql-9.5.21-1-windows-x64-binaries (1)\pgsql\bin>pg_ctl -D "C:\usr\local\pgsql\data" start
all done!
autovaccum launcher started! cheers!
Remove Postmaster file in "C:\Program Files\PostgreSQL\9.6\data"
and restart the PostgreSQL services
There are different way to open PostgreSql database .
1> One of them is by going windows and select pgAdmin4 or pgAdmin3 depends to version you use and entering password you can access you database .
2> Another one is by terminal :
To able to select from terminal you have to add the path of your installed postgresql by going enviroment variables . To do that got to installed postgresql file and select the path of bin and add to enviroment variable of window setting .
after that you can type in terminal : psql -U postgres -h localhost
Hit enter and it ask you password . After giving password you can create database and tables and can access it .
I was try to solve the problem with Windows Terminal and I've cannot to solve it. Use Windows R + cmd (if you are using Windows) for it work!
The easiest way to enable pg_ctl command is to go to your PostgreSQL directory ~\PostgreSQL\version\bin\ and execute the pg_ctl.exe. Afterwards the pg_ctl commands will be available.

Permanently set DB2CLP environment variable

I have a problem with DB2CLP environment variable, on Windows 7 64, IBM_client64_V97 (to be connected with DB2).
Every time when i try to DB2 in Windows command prompt window, I receive this error:
DB21061E Command line environment not initialized.
and per this document using just this step:
2. at the prompt enter these commands:
db2cmd -i -w db2clpsetcp
echo %DB2CLP%
I fix that error, and "db2" command opens Command Line Processor for DB2 Client.
But it lasts only until windows CMD is open. As soon as I close it and reopen, I have to repeat process.
My question (being absolute novice to DB2) is:
How should I make that change permanent and
How this happened at the first place as I installed and setup everything according to the manual (including adding local user to DB2USERS and DB2ADMINS) groups?
When DB2 client is installed, it should have installed a functional window to the command line processor. Check here:
Start / All Programs / IBM DB2 / DB2copy1/ Command Line Processor
Note that DB2copy1 is the default location for the first db2 instance. The name may be different if user select the non-default name.
Shortcut to the CLP can be copied on the desktop.
Regarding the typing "DB2 ..some command..." in windows command prompt which causes described error explanation is as follows:
That's because the normal DOS (or Windows) prompt doesn't
automatically run the setup script that enables the DB2 commands.
When the setup runs, it puts the DB2 items first in the PATH variable.
That could conflict with other tools that also want to be first on the
search list.
If you've got both the DOS (Windows) and DB2 command line prompts on
your desktop, right click them and select Properties. You'll notice
that the system (DOS) prompt runs cmd.exe.
The DB2 prompt runs DB2CMD.exe from the DB2 libraries. The DB2 script
then runs DB2SETCP.BAT and finally DB2.EXE. (All of the DB2
executables should be in C:\Program Files\IBM\SQLLIB\BIN.)
So there are differences in the two prompts. For you purposes, try
running DB2SETCP.BAT from the normal DOS prompt. That may be
sufficient for your needs.
For my purposes I always have the DOS and DB2 prompts on my desktop
and just select the one that I need.

PostgreSQL installation - error loading modules

I haven't been able to re-install PostgreSQL + PostGIS properly on our virtual server.
Installing PostgreSQL the following error occurs about the end of the process:
"A non-fatal error occurred whilst loading database modules. Please check the installation log in C:...\Temp for details."
Here is what the log says:
Loading additional SQL modules...
Executing cscript //NoLogo "C:\Programme\PostgreSQL\9.0\installer\server\loadmodules.vbs" "postgres" "****" "C:\Programme\PostgreSQL\9.0" "C:\Programme\PostgreSQL\9.0\data" 5432
Script exit code: 2
Script output:
Installing the adminpack module in the postgres database...
Executing 'C:\DOKUME~1\Kiesow\LOKALE~1\Temp\rad3C00F.bat'...
Couldn't find the output file...
Failed to install the 'adminpack' module in the 'postgres' database
loadmodules.vbs ran to completion
Script stderr:
Program ended with an error exit code
Error running cscript //NoLogo "C:\Programme\PostgreSQL\9.0\installer\server\loadmodules.vbs" "postgres" "****" "C:\Programme\PostgreSQL\9.0" "C:\Programme\PostgreSQL\9.0\data" 5432 : Program ended with an error exit code
Afterwards installation completes, I can start pgAdmin, log in and create new databases. Everything looks fine until I try to add PostGIS, using the stack builder wizard. Again the installation is almost finished, when:
"Database creation failed."
"Would you like to view the error log " "create_templatepostgis_db_error.txt'?"
This error log doesn't exist. I might ignore the error and create the template db manually, which works. But I cannot use types like 'geometry', so I think, PostGIS is not yet usable.
Maybe anyone of you has got an idea of what I could have done wrong? Any hints? Can anybody help me to interpret the error log?
I use Windows XP, PostgreSQL 9.0 32bit (same with 8.4), PostGIS 1.5.
Postgres 8.3 + PostGIS was installed on this system earlier, but I uninstalled, deleted the data directory and let the installation wizard create a new posgres windows user.
Did a few reboots, too.
And I did choose a locale instead of [Default locale] at Postgres installation.
This is another part of the log I am suspicios of. It is right in the beginning. Maybe this helps:
Could not find registry key HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql-9.0 Data Directory. Setting variable iDataDirectory to empty value
Could not find registry key HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql-9.0 Base Directory. Setting variable iBaseDirectory to empty value
Could not find registry key HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql-9.0 Service ID. Setting variable iServiceName to empty value
Could not find registry key HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql-9.0 Service Account. Setting variable iServiceAccount to empty value
Could not find registry key HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql-9.0 Super User. Setting variable iSuperuser to empty value
Could not find registry key HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql-9.0 Branding. Setting variable iBranding to empty value
Could not find registry key HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql-9.0 Version. Setting variable brandingVer to empty value
Could not find registry key HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql-9.0 Shortcuts. Setting variable iShortcut to empty value
Could not find registry key HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql-9.0 DisableStackBuilder. Setting variable iDisableStackBuilder to empty value
[15:44:31] Existing base directory:
[15:44:31] Existing data directory:
[15:44:31] Using branding: PostgreSQL 9.0
[15:44:31] Using Super User: postgres and Service Account: postgres
[15:44:31] Using Service Name: postgresql-9.0
Executing cscript //NoLogo "C:\Dokumente und Einstellungen\Kiesow\Lokale Einstellungen\Temp\prerun_checks.vbs"
Script exit code: 0
Script output:
The scripting host appears to be functional.
Script stderr:
Executing cscript //NoLogo "C:\Dokumente und Einstellungen\Kiesow\Lokale Einstellungen\Temp\postgresql_installer\installruntimes.vbs" "C:\Dokumente und Einstellungen\Kiesow\Lokale Einstellungen\Temp\postgresql_installer\vcredist_x86.exe"
Script exit code: 3010
Script output:
Executing the runtime installer: C:\Dokumente und Einstellungen\Kiesow\Lokale Einstellungen\Temp\postgresql_installer\vcredist_x86.exe
The runtime package exited with error code: 3010
From a command prompt, run this single command:
C:\Programme\PostgreSQL\9.0\bin\psql.exe -p 5432 -U postgres -W -f C:\Programme\PostgreSQL\9.0\share\contrib\adminpack.sql postgres
Its output may be more informative. Running the above is all the .vbs is doing, incidentally.
The reason for prompting this error message was due to because of localhost is pointing to some other machine.
disable firewall and antivirus and try installing it again.