What is a straightforward way to connect to Postgres.app with dbext (in Vim)? - postgresql

When I try to connect to my Postgres.app db using dbext, I get the following error:
dbext:PostgreSQL requires a '$HOME/.pgpass' file in order to authenticate. This file is
missing. The binary 'psql' does not accept commandline passwords.
Other programs connect just fine by using a "local" connection. (Postgres.app runs with my userid.)
In vim :!which psql correctly prints /opt/local/bin/psql (which I have symlinked to the one in the Postgres.app bin directory). And Postgres.app is set up to use "local" authentication and there's no clear sense of where a pg_hba.conf file would go (there is no etc directory in the app bundle). Moreover, Postgres.app doesn't have anything in its documentation about changing access configuration.
I've tried using dbext's :DBPromptForBufferParameters directly, as well as #tpope's vim-rails plugin (which returns without comment from dbext setup via :Rdbext.
So what do I do to get dbext to connect using a "local" connection?
Note - I spent a LOT of time trying to figure this out without trying the obvious, thus the post even when I already have the answer. I'm also curious to see if anyone else has a different approach.

It turns out you can just make an empty ~/.pgpass file (restricting read-write permissions to your userid only to avoid warnings). This was counterintuitive for me (since there is in fact no password), but I suppose in retrospect it's obvious I should have tried it.
I'll point this out on the dbext issue tracker.

Related

my postgres is unable to connect ever since heroku did a maintenance update 12 hours ago

anyone facing something similar? sadly I'm on a free plan so I can't open a ticket... does anyone know of a way to restart the service/machine of the DB? maybe that would just solve it...
ok so the issue in my case is that indeed heroku performed some sort of maintenance on the DB, and apparently it's connection params (host/url/user/password) had changed... and since it was embedded throughout all my interfaces (the app, my db tools, admin app I also have) - none were able to connect (they'd timeout).
while trying to figure out, I used the wonderful CLI tools of heroku pg:info and heroku pg:diagnose and even heroku pg:psql and tested to see that my data is still there... eventually I went to the online admin and that's where I saw the connections params had changed. BTW - I have this project for 2 years and this is the first time this had happened...
I was in the same situation. Heroku will email you that your database is scheduled for maintenance. After it's complete, connecting to the database fails because of this error:
error: no pg_hba.conf entry for host "IP_ADDRESS", user "DB_USER", database "DB_NAME", no encryption
This is telling us that incorrect credentials were given, meaning the database connection string has changed. I first checked the credentials on the website for the Heroku Postgres Add-on (data.heroku.com), but the connection string was still the same as before; it has not updated or changed at all, therefore this was misleading. Instead, the updated connection string is found inside of the DATABASE_URL config variable, located in the Settings tab of your app on the Heroku dashboard (dashboard.heroku.com), under Config Vars. To avoid manually correcting this problem again, get the connection string from the DATABASE_URL config variable directly, as opposed to hard coding it in your app.
Yes, Im in the same situation. Heroku are a bunch of amateurs. They did some maintenance on the DB, and after it was done, credentials don't work, even those listed in admin/dashboard section of web. Bunch of loosers... #heroku

Having Trouble with a First Time Installation of PostgreSQL 14.1 on Ubuntu 18.04

I've been having a bit of trouble trying to install PostgreSQL 14 for the first time.
I would like to apologize in advance if this question has been asked in the manner that I am about to ask it, but I do not think it has. If it has been, please direct me to the appropriate location!
I've done a fair amount of Googling on the matter, and all the information that I find seems to be rather fragmented, or I end up following a spaghetti trail of hyperlinks (a la-do-this-and-follow-this-other-link-with-more-information-than-you-need-to-understand-this-other-required-portion).
Personally, I don't want to jump around to 50 different locations on the web to try and conjure up a piecemeal solution that I believe works, only to be proven wrong later. I want to know what to do and why it works. I've tried reading the documentation, and have given up on it, because to me, it seems to assume that the server has already been set up by a database administrator.
Instead of articulating my problem directly (as I seem to be having more trouble than I would like by trying to do so), I believe it would be easier to articulate my problem indirectly by stating what my expectations would be after installing PostgreSQL for the first time.
So to start, I will mention that I'm running Ubuntu 18.04.6 LTS, and am installing PostgreSQL 14.1 with the following command:
sudo apt install postgresql-14
Before continuing, I would like to add a side note in advance, that I do not want suggestions for an alternative OS or install method. I just want to be able to get "up and running" in a common-sense fashion from this exact point.
Moving on, I know that the aforementioned command creates a *nix user called postgres.
From here, I can now indirectly state my problem using an outline of what my goals and expectations are immediately after installing the software via that command.
After installing PostgreSQL via apt, these are my expected goals:
I want any client to be able to connect to the database server from any computer where a route exists from the client to the server.
For the sake of simplicity with these stated goals, when it is directly or implicitly stated that I am trying to connect a client to the database server, I am making the assumption that the client is able to, at a minimum, ping the machine that the server is running on, and vice versa.
For now, I'm not completely worried about the database being accessible from the public Internet.
I expect to be able to access the database from any computer on my LAN, whether it is an actual LAN, or some sort of logical LAN (like a WAN or a VPN).
If I change the PostgreSQL password of the postgres user, I expect that any client logging into the database  server via the postgres user will require the password.
This means if I want to change the password to some_password via \password postgres or ALTER USER postgres WITH PASSWORD 'some_password'; (I am assuming this is how you change the login password of a PostgreSQL user), then...
I expect running psql [-h host] -U postgres -W from any host...
That when I am prompted to enter the password... 
I can only log in by entering the exact password of some_password.
Entering any other arbitrary text for the password should not allow me to log in.
I am adding this as a requirement because previous install attempts have shown me that this is NOT the case.
I expect to be able to create a PostgreSQL user account other than postgres (e.g. db_user) with a password and have it be subject to the same requirements as the postgres user.
i.e. once the new account is given permission to log in, the same common-sense login requirements to log in must be imposed, i.e. you can't get in if you don't have the correct username/password combination. 
If the process to achieve the aforementioned can be explained in such a way that it can be understood with minimal mental friction, I would be extremely grateful.
Feel free to assume that my knowledge is on par with that of a undergraduate CS student who just completed their first year of university, who also understands Linux filesystems and basic computer networking. I just want the answer to be as accessible to as many people as possible, as I am sure I'm not the only person who has struggled with installing PostgreSQL, in spite of having a power user's level of computer literacy.
sudo apt install postgresql
sudo -u postgres psql
Set a password for this user with \password or the other method you mention
sudo vi /etc/postgresql/10/main/pg_hba.conf
Make the only uncommented nonblank line in this file be host all all all md5
sudo vi /etc/postgresql/10/main/postgresql.conf
uncomment listen_addresses line and set it to '*'
sudo service postgresql restart
When you make a new user, you should also make a new database which has the same spelling as the user does. Otherwise you will need to specify the database name when you try to log in with psql -U, such as psql -U newname -d postgres -h[hhh]. Should you actually be running 14 not 10, then you will need to change the paths of the config files you need to edit accordingly.

How to setup a password for PostgreSQL in postgreapp?

I am using postgresapp for the PostgreSQL and without password I was able to connect to the database and perform operations. I am curious to learn about the password. Also I use Postico as Interface. Open to any suggestions.
The default for Postgres.app is to have no password and set trust-level authentication in pg_hba.conf. To change this, you need to do the following:
Alter the IP address and mask for host all all 127.0.0.1/32 trust as needed in pg_hba.conf, and change authentication method from trust to password or md5 (or whatever your requirements are)
Set the password for the desired user(s) with ALTER USER <username> WITH PASSWORD '<password>';
Reload the conf with SELECT pg_reload_conf()
Note your pg_hba.conf file is usually located in ~/Library/Application Support/Postgres/var-12 -- the sure-fire way to know is by querying SHOW data_directory in your psql prompt
Postgres.app is a great way to get Postgres running on macOS in a few minutes. It ships with a default user name and password, and they you're on your own. Postgres.app is a nicely compiled version of Postgres that you can run by double-clicking, you'll need other tools (and knowledge) to take advantage of Postgres. As you'll have noticed, the UI for Postgres.app is pretty much a few buttons to configure a server, and to give you shortcuts to the logs, configuration files, and data.
If you want to use psql (as mentioned), or any of the other command line tools, they're embedded in the application's package. Right-click, open the package, open Contents, open Versions, open the version you use, and look in bin.
If you want a GUI tool, there are many options. Since you say Postgres.app, I'll assume macOS. You've found Postico, SQLPro for Postgres is good, TablePlus is also good. Those tools have fairly uncluttered UIs. If you want or need more features, pgAdmin has a whole lot to offer, and it's free. I end up using Navicat a lot, even though it has a UI that screams "Look Ma! I wrote it in Java!" It gets a lot done. I'd say that day-to-day on macOS, I use SQLPro most. But, really, it's largely a matter of taste. psql is quite powerful, and you'll find no short of help for that.

How to set default connection parameters for DBLINK on Postgresql

I have Postgresql 9.5 installed on Ubuntu and need to use dblink. However I am using port 5433 (set in postgresql.conf) but dblink always defaults to port 5432. I know you can specify the port in the dblink parameters, but this is massive redundancy when I want to use the default port of my instance.
There is a way of telling dblink to use a specific port by default. I know this because I have successfully changed it in the past - but can't for the life of me remember how I did it. I have tried setting the PGPORT environment variable and rebooting. I have tried recreating the extension. I have tried reinstalling postgresql-contrib. None of this has made a difference. I have a feeling that there is some obscure file I edited but can't remember what, where or how.
Any suggestions of how to change the default connection parameters for dblink?
Got it!
It IS done by changing the environment variable, but this is not an environment variable in the sense that a Windows user would understand it (so changing /etc/environment does not work and neither does any other normal nix-style setting of global environment variables).
To change the default connection parameters for dblink you need to add PGPORT=5433 (in my case) to /etc/postgresql/9.5/main/environment. The annoying thing with Ubuntu install of Postgres is that I have at least three versions of all these sorts of files (including pg_HBA.conf and so on). The ones in /etc/... are mostly not read apart from in this case. It's the ones in my data directory (which for space reasons is in a non-default location). So, for anybody else having the same issue check /etc, /var, /your-data-directory.

Couldn't drop staging_database : #<PG::ConnectionBad: FATAL: no pg_hba.conf

For a system that I am using, I run into the problem pasted on the title when I try to drop a database and recreate it. More specifically, this is the exact problem that I am facing:
Couldn't drop staging_databse : #<PG::ConnectionBad: FATAL: no pg_hba.conf entry for host xxx.xx.xxxx.xxx, user "ruby", database "postgres", SSL off
I've done some research regarding this problem and it seems that the solution is simply to turn on SSL. I've consulted Postgresql Documentation on pg_hba.conf but I'm unable to find this configuration file.
I typed in locate postgresql and I noticed that there is a postgresql-client-9.2 installed on the system. From what I have determined, I won't find this pg_hba.conf file because the client doesn't have it. I've also looked for the postgresql.conf file on SSL according to the official documentation but this file is not included either.
Finally, the documentation shows me this configuration option of the pgsql 9.2 client shown below:
libpq reads the system-wide OpenSSL configuration file. By default,
this file is named openssl.cnf and is located in the directory reported by
openssl version -d. This default can be overridden by setting environment
variable OPENSSL_CONF to the name of the desired configuration file.
However, this too isn't in my system. I've run the linux find command and this file doesn't seem to be there. I've run out of leagues and I have a sneaking suspicion that I am overlooking something very simple. Is there any other leads I can go on? Thanks.
Based on this message and the rest of the context:
FATAL: no pg_hba.conf entry for host xxx.xx.xxxx.xxx, user "ruby", database "postgres", SSL off
It looks plausible that:
you're connecting to a remote PostgreSQL instance which you don't administrate because you're acting as developer, not admin.
the db management layer tries to connect to the database named postgres in order to drop another database (staging_database) . It's necessary indeed because we can't drop a database when we're connected to it (in fact, a database can't be dropped when anyone is connected to it).
the admin policy established by the remote pg_hba.conf is such that your login and IP address together are not allowed to connect to the database named postgres
These facts combined together imply that you're missing the necessary rights to drop your database, even if indirectly.
At this point you want to submit the problem to the admin responsible for that PostgreSQL server.