Using PostgreSQL: Difference between revisions

From ULYSSIS documentation
No edit summary
(Changes zoals uitgelegd op wikipagina documentatie herschrijven, nog wat meer nodig bij ssh)
Line 11: Line 11:
  psql -h pgsql.ulyssis.org
  psql -h pgsql.ulyssis.org


== Connecting to the database ==
=== Creating a PostgreSQL user ===
To use PostgreSQL on you ULYSSIS account, you first need to create a PostgreSQL user on [https://ucc,ulyssis,org UCC]. In the PostgreSQL section of the panel, you will be suggested to click a link to create a user. Then fill in the password for the database user. It is highly recommended that this password differs from the password you use for your account. Finally, click ''Create user'' and you are ready to go.
 
=== Creating a PostgreSQL database ===
After having created a PostgreSQL user, you can click ''Add database'' in [https://ucc.ulyssis.org UCC] and enter the name for your new database. It will automatically will be prefixed by your username.
 
=== Deleting a PostgreSQL database ===
If you no longer need a database, you can easily remove it by clicking on ''PostgreSQL'' in  [https://ucc.ulyssis.org UCC] and then pressing on ''Remove database'' next to the database you would like to delete. After confirming, the database and all its tables and other content will be gone. For security reasons, you can't remove databases using an SQL query like DROP DATABASE, but have to go through UCC.
 
== Using PostgreSQL for your website or application ==


You can connect to the database with the following details:
You can connect to the database with the following details:
Line 20: Line 29:
* Database: the database you created in UCC
* Database: the database you created in UCC


Note that some CMSs will assume that you use "localhost" as the host, and will hide the host option under advanced settings in the installation process. If you can't find anywhere to enter the hostname, look under advanced settings.
For example, if my username is foobar, I made a database called website and I were to create a PHP website I would use something like:<syntaxhighlight lang="php">
 
=== Connection string ===
 
If you are programming your application yourself, PostgreSQL often uses the following connection format:
host=$host user=$username password=$password db=$dbname
 
Where you replace the parts starting with <code>$</code> with the above connection details.
 
=== pg_connect ===
 
If you are using PHP's [http://www.php.net/manual/en/function.pg-connect.php pg_connect] to connect to the
database, and your username is "foo", your password is "password" and your database name is "foo":
<syntaxhighlight lang="php">
$connection = pg_connect("host=pgsql.ulyssis.org user=foo password=password db=foo");
$connection = pg_connect("host=pgsql.ulyssis.org user=foo password=password db=foo");
</syntaxhighlight>
</syntaxhighlight>or
=== PDO ===
 
If you want to connect using [http://be2.php.net/manual/en/intro.pdo.php PDO], your username is "foo", your password is "password" and your database is "foo":
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
$pdo = new PDO('pgsql:host=pgsql.ulyssis.org;dbname=foo', 'foo', 'password');
$pdo = new PDO('pgsql:host=pgsql.ulyssis.org;dbname=foo', 'foo', 'password');
</syntaxhighlight>
</syntaxhighlight>


==Accessing PostgreSQL from outside of our network==
=== Accessing PostgreSQL with PHPMyAdmin or Adminer ===
To prevent unnecessary load on our database server by hackers and bots it is only available inside of our network. If you wish to access it externally the easiest way is to use an SSH-tunnel. On Linux, Mac OSX and other Unix-like Operating Systems it is easily possible to use the following command to create a tunnel to a local port (in this case 5400):
Using your username and password you can easily manage your PostgreSQL databases, tables and records on https://phpmyadmin.ulyssis.org and https://adminer.ulyssis.org. Documentation is also available to aid in [[using PHPMyAdmin]] and [[using Adminer]].
 
===Accessing PostgreSQL from outside of our network===
To prevent unnecessary load on our database server by hackers and bots it is only available inside of our network. If you wish to access it externally the easiest way is to use an SSH-tunnel.
 
On Linux, Mac OSX and other Unix-like Operating Systems it is easily possible to use the following command to create a tunnel to a local port (in this case 5400):
  ssh username@ssh2.ulyssis.org -L 5400:pgsql.ulyssis.org:5432 -N
  ssh username@ssh2.ulyssis.org -L 5400:pgsql.ulyssis.org:5432 -N


You can use "ssh -f username@ssh2.ulyssis.org -L 3300:mysql.ulyssis.org:3306 -N": the f-argument will put the command in the background.
While this command is running, you can connect to the PostgreSQL server with host 'localhost' and port '5400', To terminate the port forwarding, press ctrl-C in the terminal.
 
You can use "ssh -f username@ssh2.ulyssis.org -L 5400:pgsql.ulyssis.org:5432 -N": the f-argument will put the command in the background.


== PostGIS ==
== PostGIS ==

Revision as of 16:30, 29 September 2022

Creating a database

You can create one or more PostgreSQL databases in UCC.

The first database you create will have the same name as your username. Subsequent databases will be prefixed with "username_".

Managing your database

  • The easiest way to manage your database is using phpPgAdmin.
  • You can also access it via the command line with:
psql -h pgsql.ulyssis.org

Creating a PostgreSQL user

To use PostgreSQL on you ULYSSIS account, you first need to create a PostgreSQL user on UCC. In the PostgreSQL section of the panel, you will be suggested to click a link to create a user. Then fill in the password for the database user. It is highly recommended that this password differs from the password you use for your account. Finally, click Create user and you are ready to go.

Creating a PostgreSQL database

After having created a PostgreSQL user, you can click Add database in UCC and enter the name for your new database. It will automatically will be prefixed by your username.

Deleting a PostgreSQL database

If you no longer need a database, you can easily remove it by clicking on PostgreSQL in UCC and then pressing on Remove database next to the database you would like to delete. After confirming, the database and all its tables and other content will be gone. For security reasons, you can't remove databases using an SQL query like DROP DATABASE, but have to go through UCC.

Using PostgreSQL for your website or application

You can connect to the database with the following details:

  • Host: pgsql.ulyssis.org
  • Login: your ULYSSIS username
  • Password: the PostgreSQL password you chose in UCC
  • Database: the database you created in UCC

For example, if my username is foobar, I made a database called website and I were to create a PHP website I would use something like:

$connection = pg_connect("host=pgsql.ulyssis.org user=foo password=password db=foo");

or

$pdo = new PDO('pgsql:host=pgsql.ulyssis.org;dbname=foo', 'foo', 'password');

Accessing PostgreSQL with PHPMyAdmin or Adminer

Using your username and password you can easily manage your PostgreSQL databases, tables and records on https://phpmyadmin.ulyssis.org and https://adminer.ulyssis.org. Documentation is also available to aid in using PHPMyAdmin and using Adminer.

Accessing PostgreSQL from outside of our network

To prevent unnecessary load on our database server by hackers and bots it is only available inside of our network. If you wish to access it externally the easiest way is to use an SSH-tunnel.

On Linux, Mac OSX and other Unix-like Operating Systems it is easily possible to use the following command to create a tunnel to a local port (in this case 5400):

ssh username@ssh2.ulyssis.org -L 5400:pgsql.ulyssis.org:5432 -N

While this command is running, you can connect to the PostgreSQL server with host 'localhost' and port '5400', To terminate the port forwarding, press ctrl-C in the terminal.

You can use "ssh -f username@ssh2.ulyssis.org -L 5400:pgsql.ulyssis.org:5432 -N": the f-argument will put the command in the background.

PostGIS

If necessary for an application or website you wish to use on ULYSSIS, we can always enable PostGIS for the specific database you wish to use it on. Contact us at ulyssis@ulyssis.org and let us know you would like to get PostGIS enabled and on which specific database.