How to deal with an '#' symbol in postgres password [duplicate] - postgresql

Using a Postgresql URL connection string in the format of:
postgresql://user:secret#localhost
How do I handle special characters in that string (e.g., $) so that it will actually function when I connect to my postgres database?
I've tried simply URL encoding it, so for example, "test$" becomes "test%24" ... but that seems to be a problem as I get a "FATAL: password authentication failed " error when attempting to use it.

See Connection URIs in the doc.
There are a few things that don't seem quite right in your question:
URIs are supported by postgres since version 9.2 only, so with a 9.1 client that's not supposed to work at all. Or you're using a client that implements connection URIs itself.
Percent-sign encoding is supported. Per doc:
Percent-encoding may be used to include symbols with special meaning
in any of the URI parts.
Percent-encoding is not even necessary for a dollar character.
Tried with 9.3:
sql> alter user daniel password 'p$ass';
$ psql 'postgresql://daniel:p$ass#localhost/test'
works
$ psql 'postgresql://daniel:p%24ass#localhost'
works
psql 'postgresql://daniel:pass#localhost/test'
fails as expected: bad password.

Maybe your input shell has a different encoding and $ is not %24.
Check it out on https://www.urlencoder.org/
Hint:
If you use alter user "username" password 'p$ass''word' to set/change the password,
single quotes have to be masked with another singlequote.

Related

Solr database configuration: password with special characters

I'm trying to configure Solr to connect to a postgresql database. The problem is, the password contains special characters.
]T&FV{)pO-#lpMdD (this is an example of what am trying to use as password)
I tried escaping these characters with \ but it's not working.
I also tried with http encoding %5DT%26FV%7B%29pO-%40lpMdD but it's not working either.
Solr uses JDBC to connect to your database, so escaping the password would be the same as for regular JDBC strings.
You should be able to wrap your password in quotes - "<password>" - if you're going to use http encoding, it should be supplied at the end of the connection URL (i.e. /database?password=%5DT....

FreeTDS - TSQL fails when password given in option, but succeeds when typed?

I'm trying to connect to a MS SQL Server trough PHP 5.6 with an Ubuntu 16.04 server. I'm forced to use this version of PHP, in order to assure compatibility with an 'ancient' application.
I installed PHP 5.6 and its modules (pdo, pdo_mysql, readline, etc.) trough ondrej's PPA without any trouble, but I wasn't able to find and install the 'mssql.so' module package needed for the application I'm trying to make work.
That's why I decided I would use ODBC (and PDO_ODBC) drivers in order to connect to the database.
In order to do so, I installed freetds with unixodbc and configured my files like this :
'odbcinst.ini' file :
'odbc.ini' file :
and finally the 'freetds.conf' file :
Ok, I guess all my files are well configured, but the next part starts to be a bit weird, let me explain you : when I try to connect to the MS SQL database using tsql and not giving the password in option but typing it when asked, the connexion works :
But when I try to give the same password as an option (-P), it doesn't work (I tried it at least 10 times with the correct password) !!!
--> tsql failed to open a session for the user 'WIPSOS-PHP'
The same problem happens when I try to use isql with one of my connectors I configured in the 'odbc.ini' file :
It seems to be related to the password, but I can't find the problem, can you please help me ?
I found a partial answer, and it was effectively related to the password: it seems like special characters are not well interpreted when present in the password given for the -P option of the tsql command.
Therefore, depending on you shell, you have to escape these characters in the string which completes an option. In my case, using bash, I had to use the '\' to escape the special character :
tsql -S hostname -D Database -U User -P pa\$\$word
And it now works!
But when I try to escape the special characters with '\' in the 'odbc.ini' file, it still doesn't seem to work :
My next question is : 'How to escape a special character in a '.ini' file ?' or 'Is there something wrong in my configuration ?'
EDIT: I know it was a long time ago, but I had a configuration problem related to the hostname in the "freetds.conf" file. In fact, I was using the server's local network DNS name instead of its IP adress(which works).
I hope my issue will at least help some people, it isn't easy to deal with TSQL and FreeTDS...

.pgpass file does not work as advertised

(Debian 8)
My .pgpass file is at my home folder (admin)
I and using the right format as indicated in the documentation
hostname:port:database:username:password
The same fields I put there work well when put explicitly into the psql command line. (of course I have to enter the password manually).
However, running psql by itself gives our an error:
psql: FATAL: role "admin" does not exist
Note that my sql username is NOT admin, which is my debian username.
What am I doing wrong? my goal is to get access to psql without having to use an elaborate command line including host/port/username/database
.pgpass is not a way to choose which settings you want to use, it's a way to store passwords for a number of settings you've already chosen to use. It can contain multiple lines. The relevant line is then chosen as follows, according to the documentation:
The password field from the first line that matches the current connection parameters will be used.
You still have to provide your connection parameters (besides the password).
If you always want to use the same connection parameters, you should probably use the environment variables (PGHOST, PGDATABASE, PGUSER, ...), and possibly place them in your .bashrc file (depending on the shell you use).
You can then choose to store the password itself in the PGPASSWORD environment variable or in the .pgpass file. The latter might give you a bit more flexibility.

dokku + postgres : "the scheme postgres does not accept registry part"

I'm trying to run dokku on DigitalOcean to get a ruby/rails project up with postgres.
I got help to finding the logs, but I'm at loss when I see this :
/app/vendor/ruby-2.0.0/lib/ruby/2.0.0/uri/generic.rb:214:in `initialize': the scheme postgres does not accept registry part: root:aLZgAlQKbHbKhHHn#: (or bad hostname?) (URI::InvalidURIError)
any ideas on what I could perhaps look into?
the domain name is fritida.se the databasename i fritida (or was it fritida.se?)
I'm going to go and look for a way to list databases.
if your password contains unsafe characters
Connection string used for DATABASE_URL cannot contain special characters (anything other than [a-zA-Z0-9_~-\.], e.g. # is a common one to mess up). Solution is to url-encode.
see also https://stackoverflow.com/a/34280541/1733117

What do I need to specify to get .pgpass to work?

Can someone point me to a piece of documentation that specifies the matching rules psql applies to the .pgpass file? I always spend a few extra keystrokes trying to find the right combination of host, port, database, username to get it to connect.
Some things are obvious (i.e. if my linux username is not the same as my postgres username than I need to specify it) but based on what I've seen there is some non-obvious behavior.
Here is the documentation you are probably looking for. It explains the rules pretty well.
Just so this isn't a link-only answer, here are the rules:
Each .pgpass entry is on its own line and has the following format:
hostname:port:database:username:password
You can use a wildcard (*) for each field except password
If you use wildcards, put more specific entries first
You can use for instance localhost:*:*:foo:password to specify password for user foo on your local machine regardless of the database you are connecting to or the port the Postgres is actually running on.