In the psql documentation, I read information about variables (section advanced features), e.g. one of these variables is:
HISTSIZE
The number of commands to store in the command history. The default value is 500.
Is there a file in the home directory or somewhere else where I can configure these variables?
What syntax would I use in that file?
If you look at the Files section, you'll see this:
Files
Unless it is passed an -X or -c option, psql attempts to read and execute commands from the system-wide psqlrc file and the user's ~/.psqlrc file before starting up. (On Windows, the user's startup file is named %APPDATA%\postgresql\psqlrc.conf.) See PREFIX/share/psqlrc.sample for information on setting up the system-wide file. It could be used to set up the client or the server to taste (using the \set and SET commands).
The location of the user's ~/.psqlrc file can also be set explicitly via the PSQLRC environment setting.
So like most Unix commands, there is an RC ("Run Commands") file that you can use for configuration, the name also matches the Unix conventions of ~/.cmdrc so you want ~/.psqlrc.
The format matches the \set commands you'd use within psql itself:
\set HISTSIZE 11
for example.
Related
I would like to have a .psqlrc file with default values, and be able to override these values from psql's command line.
For example :
have some values set in .psqlrc :
-- .psqlrc :
-- "user#database # " in bold green
\set PROMPT1 '%[%033[1;32;40m%]%n#%/%[%033[0m%]% > '
-- store command history in home directory, with a per database file :
\set HISTFILE ~/.psql-history- :DBNAME
in some wrapper script master-psql.sh, which connects as user postgres, be able to override these values :
# master-psql.sh :
# when using this script, change color to red, change history file location :
psql -U postgres \
-v PROMPT1='%[%033[1;31;40m%]%n#%/%[%033[0m%]% # ' \
-v HISTFILE='/some/other/place/.psql_history_postgres'
The above does not work, because the the -v ... argument is executed before the .psqlrc file is loaded, and the instruction in .psqlrc overwrites the existing value.
Question
Is there a way to instruct psql to run a set of commands after loading its .psqlrc file(s),
or to have .psqlrc execute some \set or \pset command only if value is not set ?
You could write the instructions not to overwrite those variables if already set into the .psqlrc file itself:
\if :{?HISTFILE}
\else
\set HISTFILE ~/.psql-history- :DBNAME
\endif
If you can't get your system psqlrc to cooperate with you, then might need to copy and modify it and then bypass the original. You need at least v11 for the :{? construct to work.
The problem is that PROMPT1 has a compiled-in default even in the absence of RC file processing, so you might need to test that against the compiled-in string, rather than test for being defined. So I think that would end up with something like this:
select :'PROMPT1'='%/%R%x%# ' as default_prompt \gset
\if :default_prompt
\set PROMPT1 '%[%033[1;32;40m%]%n#%/%[%033[0m%]% > '
\endif
Note that the compiled in default changed in v13, so if you want to work with older versions as well, you would need to do something more complicated.
From here:
https://www.postgresql.org/docs/current/app-psql.html
Environment
[...]
PSQLRC
Alternative location of the user's .psqlrc file. Tilde (~) expansion is performed.
So create an alternate .psqlrc file and set thePSQLRC environment variable for the script to override your default.
I connect to my db from console using command:
psql -U postgres task_db
and did this select query :
select * from common.task;
I received this Error:
ERROR: character with byte sequence 0xe5 0xb0 0x8f in encoding "UTF8" has no equivalent in encoding "WIN1252"
And followed this command from this answer to fix this: https://stackoverflow.com/a/38487921/7505731
SET client_encoding TO 'UTF8';
It worked.
Problem : Now I have to run above command every time I connect to db from command line. Is there any way I can set this encoding permanent?
You can try to set this command in your .psqlrc file:
Unless it is passed an -X option, psql attempts to read and execute commands from the system-wide startup file (psqlrc) and then
the user's personal startup file (~/.psqlrc), after connecting to the
database but before accepting normal commands. These files can be used
to set up the client and/or the server to taste, typically with \set
and SET commands.
The system-wide startup file is named psqlrc and is sought in the installation's “system configuration” directory, which is most
reliably identified by running pg_config --sysconfdir. By default this
directory will be ../etc/ relative to the directory containing the
PostgreSQL executables. The name of this directory can be set
explicitly via the PGSYSCONFDIR environment variable.
The user's personal startup file is named .psqlrc and is sought in the invoking user's home directory. On Windows, which lacks such a
concept, the personal startup file is named
%APPDATA%\postgresql\psqlrc.conf. The location of the user's startup
file can be set explicitly via the PSQLRC environment variable.
Both the system-wide startup file and the user's personal startup file can be made psql-version-specific by appending a dash and the
PostgreSQL major or minor release number to the file name, for example
~/.psqlrc-9.2 or ~/.psqlrc-9.2.5. The most specific version-matching
file will be read in preference to a non-version-specific file.
lease=# COPY dhcpd_data (ip_address, start_time, end_time, mac_address, machine_name) FROM '~/outputcsvre.csv' DELIMITER ',' CSV HEADER;
ERROR: could not open file "~/outputcsvre.csv" for reading: No such file or directory
if i define the path as '/home/rihiraj12/outputcsvre.csv', it works fine.
Yes, that's normal.
You don't really have a directory called ~. When you execute a command on the command line, the shell will expand ~ to /home/rihiraj12 before running the program. But here you're not using the shell, so ~ is interpreted literally.
As a workaround you could say
COPY dhcpd_data (...) FROM PROGRAM 'cat ~/outputcsvre.csv' ...
But note that the COPY command is executed by the server, so this will make the server spawn a cat command and use the home directory of the PostgreSQL server.
To specify the file from your own point of view, you can (in psql) use the \copy meta-command (which has the same syntax as COPY):
\copy dhcpd_data (...) FROM PROGRAM 'cat ~/outputcsvre.csv' ...
This will use your own home directory as ~.
~ is a shortcut that unix-like shells can expand to be the home directory of your user.
i.e. if you use ~/outputcsvre.csv , the shell converts this to /home/rihiraj12/outputcsvre.csv before doing anything else with it.
Outside a shell, applications rarely implement this expansion - and neither does postgresql, so you have to provide real path to the file.
In the case of the COPY command in postgresql, it is executed by the server - so in this case you will have to provide a filename that the server can resolve and read directly. (i.e. a relative path would be relative to wherever the postgresql server is located - so use an absolute path for the file.)
If I set PGPASSFILE to an explicit path like /home/user/.pgpass then it works fine and when logged in as the user that owns that file I can use psql for the entries in .pgpass.conf.
The problem I have is that I need to have multiple accounts use psql. If I change PGPASSFILE to user directory like ~/.pgpass.conf then it doesn't work and doesn't read the file so it gives a password error.
Because I can only specify one file it means only the owner of that file can run the commands I need to run.
I am running on Ubuntu 18.04 and I need root & www-data to have a .pgpass.conf file.
How do I do this?
If you have system users corresponding to your db users (root and www-data in your case), each has its own, separate .pgpass file in its respective home directory. Set each accordingly.
And simply do not set PGPASSFILE at all. The manual:
PGPASSFILE behaves the same as the passfile connection parameter.
And:
passfile
Specifies the name of the file used to store passwords (see Section 33.15). Defaults to ~/.pgpass, or
%APPDATA%\postgresql\pgpass.conf on Microsoft Windows. (No error is
reported if this file does not exist.)
Related:
Run batch file with psql command without password
What is the command-line equivalent of "Switch Port Client User" as found in the p4win gui client?
I am already logged under one port but now I am attempting to connect to a different port on the same server in order to access a separate source control file depot. I assume it would involve using:
p4 login
However, reading the 'help' for 'login' does not show an option to specify the port #. Both user name and client name would remain the same but just need to change the port #.
The P4PORT configuration variable stores the Perforce server name and port number to connect to. You can set this value as an environment variable or, if you're using Windows, in the registry using 'p4 set':
p4 set P4PORT=perforce:1669
To see what the current value of P4PORT is:
> p4 set P4PORT
P4PORT=perforce:1669
If you want to do it generically for any P4 command then the general form can be found via "p4 help usage".
In a nutshell,
p4 -p <your port> login
will do what you asked for. Note from the usage help that you can specify most things from the command line such as client spec, username, password, etc.
E.g:
p4 set P4PORT=1666
From the help:
C:\> p4 help environment
Environment variables used by Perforce:
Variable Defines For more information see
-------- ------- ------------------------
P4AUDIT name of server audit file p4d -h
P4CHARSET client's local character set p4 help charset
P4COMMANDCHARSET client's local character set for
command line operations p4 help charset
P4CLIENT name of client workspace p4 help client
p4 help usage
P4CONFIG name of configuration file Command Reference Manual
P4DIFF diff program to use on client p4 help diff
P4DIFFUNICODE diff program to use on client p4 help diff
P4EDITOR editor invoked by p4 commands p4 help change, etc
P4HOST name of host computer p4 help client
p4 help usage
P4JOURNAL name of server journal file p4d -h
P4LANGUAGE language for text messages p4 help usage
P4LOG name of server log file p4d -h
P4MERGE merge program to use on client p4 help resolve
P4MERGEUNICODE merge program to use on client p4 help resolve
P4PAGER pager for 'p4 resolve' output p4 help resolve
P4PASSWD user password passed to server p4 help passwd
P4PORT port client connects to p4 help info
or server listens on p4d -h
P4ROOT server root directory p4d -h
P4TARGET target server for proxy Command Reference Manual
P4TICKETS location of tickets file Command Reference Manual
P4USER user name p4 help usage
PWD current working directory p4 help usage
TMP, TEMP directory for temporary files Command Reference Manual
See 'p4 help set' for details specific to Windows. The syntax for
setting an environment variable depends on the OS/shell. Note that many
shells allow the setting of shell variables separate from environment
variables - Perforce cannot see the shell variable, only the environment
variable.
If you are a typical user then the only variables of interest are
$P4CLIENT, $P4PORT and $P4PASSWD.
C:\> p4 help set
set -- Set variables in the registry (Windows only)
p4 set [ -s -S service ] [ var=[value] ]
'p4 set' sets the registry variables used by Perforce on Windows
platforms. Normally, the variable 'var' is set to 'value'.
If 'value' is missing, the variable 'var' is unset. Without
any arguments at all, 'p4 set' list variable settings.
The -s flag causes 'p4 set' to set variables for the whole system
rather than for the user. You must have NT administrator powers
to use this.
The -S service flag causes 'p4 set' to set variables for the named
service. You must have NT administrator powers to use this.
Currently, registry variable entries may be overridden by environment
variables and (in some cases) flags on the command line.
See 'p4 help environment' for a list of environment/registry variables.
You can use a configuration file to set the port that Perforce connects to for each project.
First, create a text file that contains the Perforce configuration variables you want to set for the project. For example, to set the value of P4PORT, the contents of the file would look like this:
P4PORT=hostname:1234
Name the file something descriptive like .p4config and place it in the root of the project folder. Do this for each one of your projects, changing the variables as necessary. Use the same filename.
Then, set the value of P4CONFIG to the name of your configuration files, e.g.
p4 set P4CONFIG=.p4config
This will make Perforce look for configuration values in a file of that name in the current directory or any parent directory, so you don't have to change your configuration variables manually every time you switch projects.