Access embedded postgreSQL database - postgresql

Can an embedded postgreSQL 9.1.1 database be simultaneously accessed externally by another application?
I installed a separate postgreSQL server on the same machine, but the embedded database doesn't seems to be visible.
The other problem is that I cant find out the password for the user that the application is using to connect to its internal database file.

If you want to connect to a postgresql server, that was installed on you machine by some software you need:
1) Find the cluster directory of the installed postgresql server.
2) Search for the postgresql.conf file in cluster directory. There you will find a port, the server is listening to.
3) Search for pg_hba.conf file in cluster directory. Adding host all all 127.0.0.1/32 trust line to this conf file will allow you to access the server without password.
4) Start the postgres server, specifying the cluster directory from step 1 . (Search for postgres binaryes, installed by the application.). If it is already running - you need to restart it.
5) Connect to the DB using post from step 2 and any postgres login (Try login postgres it is a default one)

Related

Golang and accessing postgres via a client in an Ubuntu VPS?

I'm trying to follow the digital ocean tutorial on configuring pgadmin4 in server mode, but damn it is long, and I have to first configure apache server, python and virtualenv (via other 2 tutorials).
I don't want to install so many dependencies in my server just to access postgres via pgamin 4.
How do you guys do it?
I'm running a go webserver via https listening on ports 443 and redirecting 80 to 443
Seeing your other answer I would like to offer a more secure alternative.
What's wrong with the current approach?
Your PostgreSQL instance is accessible from the internet. Generally you should try to limit access only where it is required. Especially if you are not using SSL to connect to PostgreSQL, an open port like this is a target for traffic interception and brute force attacks.
Alternative
Seeing that you are you using JetBrains IDE's you only need one other step to access your data - setting up a SSH tunnel.
This encrypts with SSH all your connections between development host and server without exposing PostgreSQL to the outside world.
In the connection settings for your database in the Jetbrains IDE select the SSH/SSL tab and "Use SSH tunnel". Input the information of your server and the SSH user + password/SSH key (use SSH keys for better security) into the relevant input fields.
Undo the settings changes you did to open the firewall and configure PostgreSQL to listen to all nodes.
Connections to your database are now possible over encrypted tunnels without exposing your database to any unwanted attacks.
So this is what I did to achieve connection from my laptop to my ubuntu VPS, via webstorm (I suppose any intellij works also should work with other IDE's)
0 login to your server
1. Locate postgresql.conf usually under /etc/postgresql/10/main
2. sudo nano postgresql.conf
3. Locate and change line at connections
listen_addresses = '*'
Then in same dir edit: sudo nano pg_hba.conf
#TYPE DATABASE USER ADDRESS METHOD
host all all 0.0.0.0/0 md5
Md5 means I connect with user and his password
5 Dont forget to allow ufw (firewall)
sudo ufw allow 5432/tcp
Open webstorm > Database (tab) > click + to add PostgtresSQL source (fill relevant info, user name, password, database name, host and port, etc...)
jdbc:postgresql://example.com:5432/my_database_name
Press on schemas and synchronize OR press:
Source > Settings > Schemas tab > [check] All Databases > refresh

Accessing WSL postgresql server from Windows PGadmin

I am running postgresql in WSL Ubuntu on windows. Everything is up to speed, my data is loaded and I wish to access the database through some graphical interface. I was thinking pgadmin4.
Is it possible to accomplish this through a windows install of pgadmin4? I installed pgadmin4 on windows and tried to connect the traditional way in the GUI through localhost but am not getting a connection. I figure there may be a special method here.
For everyone else stumbling across this: The best way to do this (that I know of) is to SSH into your local WSL and then configure the SSH in pgAdmin to that.
I have yet to figure out, how to use this remotely.
As long as postgres is running within your wsl2 instance (check with sudo service postgresql status) then within PgAdmin 4 (running in windows), all you need to do is click to "Register" a new server.
Then, while entering the Connection data, set host to localhost and port to 5432 (unless you specified unique port within your postgres instance when creating in wsl2.
See this answer for more detail
There is no special way needed, you should be able to get a connection. Just like your dev http ports are exposed to your browser on Windows, your db port should too.
I had to manually add localhost to pg admin though which is a bit weird.
Make sure your db service is up and running on Ubuntu, sometimes the db service is killed for no reason.
To see if your PostgresSQL service is up or not:
sudo service postgresql status
If it's not, start the service:
sudo service postgresql start

Create new local server in pgadmin?

I have PostgreSQL 11 and PGadmin 4 installed on windows. Currently I'm connected to a AWS server which hosts all of my data.
I want to create a local server (localhost) as a testing environment where I can experiment. I can't seem to do it though, and the other similar questions on stack don't help. Here's what my process is:
in pgAdmin, right click 'Servers' and go Create>Server
On the 'Create - Server' pop up box, i type in Name: Localserver. For 'connection' I type localhost. Port I leave as default '5432', db: postgres, username: postgres password: empty
click save.
however, I get an error:
Unable to connect to server:
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
What should I do? I am the admin if that makes a difference.
As a new Postgres user, I did not understand how to make use of Postgres on Ubuntu. So I'm just going to chime in and help out other newbies who perhaps cannot figure out how to work with Postgres on Linux. If you're using Windows, steps should be similar.
Before you get to using PgAdmin, a bit of configuration is required. Most will happen in your terminal at first.
Open a terminal using Ctrl + Alt + T if you're on a PC. Or just pres ALT + F1 and begin typing Terminal.
Let's start with the basics first and make sure you have proper
installation.
1. Installing Postgres Latest
1.1 update the system software packages
sudo apt update
1.2 install latest version of PostgreSQL from default Ubuntu repositories
sudo apt install postgresql
the installer will create a new PostgreSQL collection of databases
that will be managed by a single server instance
Default data directory : /var/lib/postgresql/your-version/main
Configurations files : /etc/postgresql/your-version/main
2. Checking if Postgres Service is Installed
2.1 Check if Postgres is Active
sudo systemctl is-active postgresql
You should see : active
2.2 Check if Postgres is enabled
sudo systemctl is-enabled postgresql
You should see : enabled
2.3 Check Postgres Service status
sudo systemctl status postgresql
You should see : active (exited) marked in green
2.4 Check if Postgres is ready to accept connections
sudo pg_isready
You should see : /var/run/postgresql:5432 - accepting connections
3. Configuring Postgres Authentication
3.1 Opening the pg_hba.conf as SUPERUSER
sudo code --user-data-dir=~/root /etc/postgresql/13/main/pg_hba.conf
I'm using visual studio code so for me code is vsc codename. If you're using vim or sublime just replace code with your text editor name.
3.2 Configuring pg_hba.conf
Notes: you shouldn't need to change anything here, just make sure your
configuration files matches the following lines :
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
Hit save and close.
3.3 Restart Postgres Service
sudo systemctl restart postgresql
4. Create NEW Server
For me, this is where all my confusion was. Before you use PgAdmin,
you need to create a server in your terminal, then you can connect and
manager it with PgAdmin just like you would with PhpMyAdmin. It's
actually easier.
4.1 Access the PostgreSQL database shell
sudo su - postgres
psql
You will then see this : postgres=#
4.2 Creating new server and user
postgres=# create user bob with superuser password 'admin';
That's how you create new user and server in Postgres. Let's move on to PgAdmin.
5. Installing pgAdmin4
5.1 Add public key for the repository
curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo apt-key add
Notes : if you don't have curl your Ubuntu will give you the command to install it
5.2 create the repository configuration file
sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'
5.3 install pgAdmin4
sudo apt update
sudo apt install pgadmin4
5.4 run the web setup script installed with the pgadmin4 binary package
sudo /usr/pgadmin4/bin/setup-web.sh
It will asks you for your email address and password. This email and
password is required to login to the PgAdmin4 web interface
6. Accessing pgAdmin4 Web Interface
6.1 Open your favorite browser
type in the address of the PgAdmin web interface. It usually looks like this
http://127.0.0.1/pgadmin4
Note : After installation the web setup script will tell you exactly
where to access the web interface.
When you see the login screen, enter the email and password you've chosen during the web setup script.
6.2 Adding New Server Connection
6.2.1 Click on Add New Server
6.2.2 Under General tab enter a name for your server. ANY name you would like, it doesn't matter. You could enter PgServer1 or whatever. Don't change anything else.
6.2.3 Switch to Connection Tab
Hostname/ Address : localhost
Port : 5432
Maintenance database : postgres (always)
Username : **bob** (the username youve chosen at 4.2)
Password : admin (or any password you chose at 4.2)
Hit Save.
Voila! You should be connected successfully. If not, just open terminal and create a new user like we did at 4.2
Notes : to create databases is very easy just right click on your
servername
create > databases
Useful Resources & Tutorials
PostgreSQL Fundamentals : Queries and the likes
PostgreSQL & JSON : useful for dealing with JS apps
PostgreSQL & Nodejs : Create realtime apps with nodejs and socket.io
More PostgreSQL Nodejs
https://github.com/supabase/realtime
https://whatsyourssn.com/posts/real-time-app-socket-postgresql/
UPDATE 2023
While following my own tutorial I ran into certificate issues at step 5 when I tried to create the repository file. The full error I was getting was this.
I want to post the solution that worked for me here.
This is probably one of two things.
You have set up a Postgres server, but have not adjusted listen_addresses in your postgresql.conf file, or
You have not set up a Postgres server on your local machine. ("Create Server" is a bit misleading, it should probably be "Create Server Connection".)
Usually a “connection refused” error indicates that the database server is either 1) not running, or 2) configured in such a way that it is not listening to the right port or IP address. Be sure to check ps -ef to see if Postgres is running, and also look at postgresql.conf to see if port and listen_addresses are set properly.
make sure the postgres service is running.
example in Linux : systemctl enable postgresql.service
Why? Attempting to connect to the server that is not running.
Action: Start your PostgreSQL server.
The path in the command below points to the data directory configured during the installation of PostgreSQL. Run this in Windows cmd.
pg_ctl -D "C:\user\PostgreSQL\data" start
>server started
After this go to pgAdmin and follow the initial steps as in question.
Create Server
Under General: Name: mytestServer
Under Connection: Hostname: localhost, Password: yourPassword,
keep other settings as default
Hit save
If you haven't, then first install the Postgres server on your machine. For windows, this is the currently active link for downloading the installation package: https://www.postgresql.org/download/windows/
As other answers here pointed out, PgAdmin is only a interface for using the Postgres server. Once you install the server locally, you'll be able to see the server on the left hand side pane after restarting PgAdmin.

Postgres and Atlassian Jira: driver issue

I am trying to setup a server with centOS 32 bit to install Atlassian Jira on it.
I followed the official Atlassian installation guide at https://confluence.atlassian.com/display/JIRA/Installing+JIRA
Now I am running the Setup Wizard and I need to configure a PostgreSQL database. On my centOS, I installed version 8.4.20 via yum.
However, I am having a hard time setting Jira. Postgres is running and I can login via Linux console, but when I test the connection to the database, I get the following error:
Error connecting to database
Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection refused
Also, this error appears when I type the full public IP address. If I insert localhost, I get this:
Error connecting to database
FATAL: Ident authentication failed for user "jiradbuser"
If I insert
http://<public ip>
I get the following:
Error connecting to database
No suitable driver found for jdbc:postgresql://http://<public ip>:5432/jiradb
However, I put my jdbc driver into /opt/atlassian/jira/lib and its name is postgresql-8.4-703.jdbc4.jar. Postgresql version is 8.4.20.
Where am I doing wrong?
Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
By default, PostgreSQL listens only on the local loopback interface, so connecting to the public IP won't work. If you're connecting from localhost you don't need to anyway, just use localhost.
FATAL: Ident authentication failed for user "jiradbuser"
This is good, it shows that you're connecting to PostgreSQL successfully, then getting an authentication error when trying to log in.
Your PostgreSQL server install defaulted to ident authentication for TCP/IP connections, but the JIRA application isn't running under a unix user named "jiradbuser" so the connection is rejected. Change pg_hba.conf to use md5 instead of ident and set a password for the user. See the client authentication chapter in the docs, particularly, pg_hba.conf.
No suitable driver found for jdbc:postgresql://http://:5432/jiradb
I have no idea where you got the idea that that URL would work...
You want something like:
jdbc:postgresql://localhost:5432/jiradb
I had the same problem. I could solve it setting up the postgres driver .jar from maven repository to \target\jira\webapp\WEB-INF\lib\
Pretty late to the question here but thought I'd add a couple bits to help out the future, going backwards:
No suitable driver found for jdbc
A while ago Atlassian didn't distribute the PostGreSQL JDBC driver file with the JIRA releases. They have been doing this for a while now (I'd say since mid 2013) - regardless, the driver file belongs in the <jira-base>/lib directory unless you are doing a WAR install, in which case you shouldn't, and just put the driver file in the lib directory anyway
FATAL: Ident authentication failed for user "jiradbuser"
1. Listen on a socket if you need to - The best approach to this is to look at your JDBC driver URL - if it has an IP/hostname other than 127.x.x.x or 'localhost' you need to modify PostGreSQL to listen to a TCP port by modifying your postgresql.conf file to enable listening on ports. Just find the file, back it up, then open it and search for localhost, read the file comments and make the correct change. If you find yourself looking around in multiple locations in the file or changing more than a single word you are trying too hard - restore your backup and try again. Just remember to *stop* and *start* your postgres db cluster when you are done
2. enable the correct login METHOD in pg_hba.conf (same directory as the postgresql.conf file - on unix typically under /etc/postgresql/<version>/main/.) - this is difficult to writeup in brief but I'll try
1. backup the pg_hba.conf file - I ain't foolin, this is something you'll be happy with later - back the sucker up
2. edit the file
3. go to the bottom of the file - look for the last line like "# TYPE DATABASE USER ADDRESS METHOD"
4. comment out every line under this 'table heading' line
- the reason to do this is that the package maintainers often fall back to commenting here and so you'll have 2 commented out lines and then one line that isn't commented out - and you'll never notice it - it's easiest to just start off with commenting out the whole block of stuff so there isn't a single uncommented line in the file
- add a two lines for the postgres user to connect local and to a db socket unless your corporate security does not allow - something like this:
local all postgres 127.0.0.1/32 peer
host all postgres 127.0.0.1/32 trust
- add a line for your jiradbuser to connect to the newly created database via sockets with md5 encryption. md5 isn't the best - there are other options to google - if you use md5, it'd look something like this:
host jiraversiondb jiradbuser 127.0.0.1/32 md5
Then save the file, restart postgres cluster (with a stop and start, not with the 'restart' option), and test connecting from the command line.
How to test connections from the command line
If you are logged into a unix system with the postgres tools installed you should be able to mimic the connection attempt which the JIRA server will try to connect to the database. If you cannot connect to the new postgresql db from the command line it's a REAL GOOD chance that JIRA won't be able to either. Plus you'll get better error messages. So to do this just bring up a shell and enter (replacing variables where appropriate - things within the '<' and '>' characters) - all the values for these are in your head and the postgresql.conf file:
psql --port <port - default is 5434> --username=<db user name> --password --dbname=<database name>
Once you can connect from the command line it's a good bet JIRA will as well - no promises, but you'll be on sound footing.
...ahhh....now, that'll hopefully help someone...

How to connect Pgadmin3 to a database on Virtualbox machine?

I have an Ubuntu Server with Postgresql server on Virtualbox machine (which runs on local machine), and Pgadmin3 on local machine.
Now how can I connect with Pgadmin3 to a database on Virtualbox? I am getting
"Server doesn't listen"
Postgres is actually running inside Virtualbox.
Found this out. To make it work, i did:
1) Edited file /etc/postgresql/9.1/main/postgresql.conf , uncommented and edited one line to contain:
listen_addresses = '10.10.4.14,localhost'
Where 10.10.4.14 is address used to access server on virtualbox.
2) Edited file /etc/postgresql/9.1/main/pg_hba.conf , added one line at the end:
host all all 10.10.4.14/24 md5
Note file path in your case might be different if your Postgres version is not the same as mine, or another linux distribution might place those files in other locations.