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
Related
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.)
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.
I've created an installer for my application that also installs postgreSQL.
The first question:
What is the best way to configure my database, I mean, is it better to use the postgres default user account? Or should I create a new user. Also is it better to use default public schema or should I create a new schema? As one can see, I'm seeking the best practices to use databases. Also, what kind of care do I need to take with possible already installed versions of postgreSQL from any other application?
The second question:
How to create a user using the installer?
I'm trying to create a new user different from the default user but how can I set a password for this user? I would like to do this without prompting the user in a command shell.
If it's helpful, I will happily post any of my code!
Let's go for some points that you've asked in your question.
What is the best way to configure my database, I mean, is it better to use the postgres default user account?
That doesn't matter at all as long as you have defined a strong password for the user to install it you will be good.
is it better to use default public schema or should I create a new schema?
That will depends on your software. If you want to have everything well defined and organized on the use of the database go on, create a schema to your system. If your system will be the only one using PostgreSQL why botter? Read this answer on this matter from DBA forum PostgreSQL and default Schemas
Also, what kind of care I might to take with possible already installed versions of postgreSQL of any other application?
That's a trick one and the very first problem you will have. I find out the hard way that you can't install PostgreSQL database if you already have one previously installed. Not even if you uninstall it (in this case you have to delete all associated registries from windows registry of PostgreSQL).
My advice here is that you put a message on your installer that your system needs a fresh install of PostgreSQL on the target machine and it can't have a previously installed one on it otherwise the install will fail. I know that sucks, but I couldn't find anything to do here. One other thing that I did was to add a note on the installer messages to the user that if He still want to install the system on that machine He would have to contact the "support" to do it manually.
How to create a user using the installer?
From here I will add some commands to you install PostgreSQL in unattended mode.
I'm trying to create a new user different from default user but how to set a password to this user?
The command you will need to install PostgreSQL with a user different from the default one will be:
postgresqlInstallFile[withversion].exe --mode unattended --superaccount super_user_name --superpassword yourStrongPassowordHere
It would be great here if you could ask for the password on your installer program and use a variable within above command.
That alone will install the database with a user defined by you. Here is the documentation about all parameters for PostgreSQL Unattended Install
The second problem is regarding the database of you system. The best course of action I could find was to make a Dump file from the database that I need to install (this database would be empty, with just the structure), added this dump file to the installer so I could import it to the fresh installation of the database.
The problem with this approach is you can't create a database or restore it to PostgreSQL without entering the password for the super account. What I did was
Add messages to the user that He should pay attention to the installer messages and steps so he can't get it wrong
After installing PostgreSql database comes the step to create the database so I use the command:
[path_for_postgresq]/createdb.exe -U user_you_created -W -E UTF8 -O user_you_created "NameOfYourDatabase"
Before I run the above command I showed a message to the user that he will see a prompt screen asking for a password, so He have to enter it and hit Enter. You can't add it as a parameter (PostgreSQL says it is for security measures)
After the database creation it is time to import the dump that I've added on the installer so I used:
[path_for_postgresq]/pg_restore -U user_you_created -c -d NameOfYourDatabase DumpFileOfYourDatabase.backup
Once again, before this command I showed a message to the user asking to enter the password for the database restore command.
Here I will add the code I made for the NSIS installer. Just the PostgreSQL part of it. There are a lot more on my installer you may need something more.
;--------------------------------
;Include Modern UI
!include "MUI.nsh"
;Include Process checker
!include nsProcess.nsh
;Include ZipDLL
!include zipdll.nsh
; include for some of the windows messages defines
!define ALL_USERS
!include winmessages.nsh
;--------------------------------
;General Configuration
Name "Name of your Installer"
OutFile "fileNameOfYourInstaller.exe"
ShowInstDetails "show"
InstallDir $PROFILE\FolderWhereToInstallYourSystem
; Request application privileges for Windows Vista
RequestExecutionLevel admin
; HKLM (all users) vs HKCU (current user) defines
!define env_hklm 'HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"'
!define env_hkcu 'HKCU "Environment"'
;--------------------------------
;Interface Settings
!define MUI_ABORTWARNING
;--------------------------------
;Pages
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
;--------------------------------
;Languages
!insertmacro MUI_LANGUAGE "English"
Function .onInit
SetOutPath $TEMP
# the plugins dir is automatically deleted when the installer exits
InitPluginsDir
File /oname=re.bmp re.bmp #here is an image for a splash screen of the installer
splash::show 2000 re
Pop $0 ; $0 has '1' if the user closed the splash screen early,
; '0' if everything closed normally, and '-1' if some error occurred.
FunctionEnd
Function verifyInstallationDIR
IfFileExists $INSTDIR PathGood
MessageBox MB_OK "The chosen directory is not valid for installation.$\r$\nPlease start again and inform a valid path."
Quit ;if $INSTDIR wasn't created the installer will close
PathGood:
FunctionEnd
Section
SetOutPath $TEMP
CreateDirectory $INSTDIR
#Message to the user to PAY ATTENTIOM
MessageBox MB_OK "Please carefully read all messages through installation steps!"
Call verifyInstallationDIR
#Files needed to the installer
File "resources\DatabaseDumpFile.backup"
File "resources\postgresql-9.3.9-3-windows-x64.exe"
#Call postgresql installation in an unattended mode
ExecWait 'postgresqlInstallFile[withversion].exe --mode unattended --superaccount super_user_name --superpassword yourStrongPassowordHere'
Postgresql_Running_Check:
; is app.exe process running? result is stored in $R0
${nsProcess::FindProcess} "postgres.exe" $R0
${If} $R0 > 0
MessageBox MB_RETRYCANCEL|MB_ICONEXCLAMATION "PostgreSQL process is not running yet. Wait 30 seconds and click Retry or Cancel to cancel installation." /SD IDCANCEL IDRETRY Postgresql_Running_Check
Quit
${EndIf}
;Create the database
MessageBox MB_OK "The System DataBase will be created. A console window will appear asking for a password. Please when it happens type:$\r$\n$\r$\nyourStrongPassowordHere"
ExecWait '$PROGRAMFILES64\PostgreSQL\9.3\bin\createdb.exe -U user_you_created -W -E UTF8 -O user_you_created "NameOfYourDatabase"'
MessageBox MB_OK "The System Installer will import the configuration data to the database. It will ask again for the password. Please when it happens type:$\r$\n$\r$\nyourStrongPassowordHere"
ExecWait '$PROGRAMFILES64\PostgreSQL\9.3\bin\pg_restore -U user_you_created -c -d NameOfYourDatabase DatabaseDumpFile.backup'
SectionEnd
Hope it helps.
mysql-workbench, how do I write a bash file command as to launch and open a SQL script to a query tab? I am not sure which parameter I need to pass.
thks
ps. Now I am on a Fedora 32. (kind of very late edit, sorry)
You should always specify which OS you are using.
Generally, you can start MySQL Workbench with various command line paramaters. One of them is --help (-help on Windows). This will give you the following list:
MySQLWorkbench [<options>] [<model file>]
Options:
--query [<connection>] Open a query tab to the named connection or prompt for it if none given
--admin <instance> Open a administration tab to the named instance
--upgrade-mysql-dbs Open a migration wizard tab
--model <model file> Open the given EER model file
--open <file> Open the given file at startup
--run <script> Execute the given code in default language for GRT shell
--run-python <script> Execute the given code in Python
--run-lua <script> Execute the given code in Lua
--migration Open the Migration Wizard tab
--quit-when-done Quit Workbench when the script is done
--log-to-stderr Also log to stderr
--help, -h Show command line options and exit
--log-level=<level> Valid levels are: error, warning, info, debug1, debug2, debug3
--verbose, -v Enable diagnostics output
--version Show Workbench version number and exit
(this is for OS X and Linux, Windows uses a single dash instead). For OS X you also have to use the direct call. So instead of:
open /Applications/MySQLWorkbench.app --args --help
which will not show you the parameters use:
/Applications/MySQLWorkbench.app/Contents/MacOS/MySQLWorkbench --help
To open a connection and load an SQL script file use the following parameters:
... --query "Localhost 5.6" --open /Data/Work/Data/Scripts/alter_test.sql
Again, on Windows use single dashes. For the query parameter use the name of a connection you have defined.
I am building a Windows 7 installer with NSIS for internal company use.
One step in the install is to backup a Postgres DB using pg_dump.exe, then restore a file packaged in the installed usign pg_restore.exe. These two executables are normal helper tools include with any Postgres installation, so they can be expected to exist on the user's machine.
I want to execute this operation like so:
ExecWait 'C:\path\to\file\pg_dump.exe --host localhost --port 5432 --username "postgres" --no-password --format custom --blobs --verbose --file $OUTDIR/myDB.backup myDB' $0
DetailPrint 'Backup DB process return $0'
But the problem I'm having is that the path of executables differs from machine to machine. So I am trying to run a search operation to find the files. Such as this
${Locate} "C:\" "/L=F /M=pg_dump.exe" "locateBackup"
Or this (using locate plugin)
${locate::Open} "C:\" `/F=1 /D=0 /X=exe /-PN="Oracle" /-PF="$WINDIR\System|$WINDIR\System32|$WINDIR\Help" /N=pg_dump.exe /B=1` $0
${locate::Find} $0 $1 $2 $3 $4 $5 $6
Both of those methods fail on Windows 7. The installer crashes when the search traverses certain directories; I'm not sure why.
Next thing I'd like to try is to look up the executables location in the Windows registry, with the NSIS command ReadRegStr, maybe something like this
ReadRegStr $0 HKLM Software\pg_dump ""
DetailPrint "Its installed at: $0"
But I can't find the right registry key for looking up pg_dump (or pg_restore).
Is there a way to identify the path of installed executables on Windows 7 using native NSIS functions?
Or is there a better way to backup and restore Postgres databases with NSIS installers?
You can find the installation directory (if it was installed through the one-click installer) in
HKLM\PostgreSQL\Installation\postgresql-9.1\Base Directory
apparently this key is version dependent. And there can be more than one entry (if more than one version is installed). So you will need to enumerate the entries and pick whatever version fits you.
Then append \bin to the value obtained from "Base Directory" and pg_dump/pg_restore should be there.